2019-07-15 12:37:18 +00:00
|
|
|
Scheduling tasks
|
|
|
|
================
|
|
|
|
|
|
|
|
Pyrogram itself as Telegram MTProto API Framework contains only stuff
|
|
|
|
related to Telegram. Scheduling is out of it's scope.
|
|
|
|
|
|
|
|
But it is easy to integrate pyrogram with your favourite scheduler.
|
|
|
|
|
|
|
|
schedule
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import time
|
|
|
|
import schedule
|
|
|
|
|
|
|
|
|
|
|
|
def job():
|
|
|
|
app.send_message("me", "Hi!")
|
|
|
|
|
2019-08-01 07:11:29 +00:00
|
|
|
schedule.every(3).seconds.do(job)
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
with app:
|
|
|
|
while True:
|
|
|
|
schedule.run_pending()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2019-08-01 07:11:29 +00:00
|
|
|
Note that schedule is not suitable for async version of pyrogram.
|
|
|
|
For more information read `library <https://schedule.readthedocs.io/>`_ docs.
|
2019-07-15 12:37:18 +00:00
|
|
|
|
|
|
|
apscheduler
|
|
|
|
-----------
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import time
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
|
|
|
|
|
|
|
def job():
|
|
|
|
app.send_message("me", "Hi!")
|
|
|
|
|
|
|
|
|
|
|
|
scheduler = BackgroundScheduler()
|
|
|
|
scheduler.add_job(job, 'interval', seconds=3)
|
|
|
|
|
|
|
|
scheduler.start()
|
|
|
|
app.run()
|
|
|
|
|
|
|
|
Apscheduler supports async version of pyrogram too, here is async example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
|
|
|
|
|
|
|
|
async def job():
|
|
|
|
await app.send_message("me", "Hi!")
|
|
|
|
|
|
|
|
|
|
|
|
scheduler = AsyncIOScheduler()
|
|
|
|
scheduler.add_job(job, 'interval', seconds=3)
|
|
|
|
|
|
|
|
scheduler.start()
|
|
|
|
app.run()
|
|
|
|
|
2019-08-01 07:11:29 +00:00
|
|
|
For more information read `library <https://apscheduler.readthedocs.io/>`_ docs.
|