Skip to content

Commit 6973f58

Browse files
authored
Update scheduling.rst
1 parent 93a2fed commit 6973f58

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

docs/source/topics/scheduling.rst

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,87 @@
1-
Scheduling tasks
1+
Scheduling Tasks
22
================
33

4-
Pyrogram itself as Telegram MTProto API Framework contains only stuff
5-
related to Telegram. Scheduling is out of it's scope.
4+
Scheduling tasks means executing one or more functions periodically at pre-defined intervals or after a delay. This is
5+
useful, for example, to send recurring messages to specific chats or users.
66

7-
But it is easy to integrate pyrogram with your favourite scheduler.
7+
Since there's no built-in task scheduler in Pyrogram, this page will only show examples on how to integrate Pyrogram
8+
with the main Python schedule libraries such as ``schedule`` and ``apscheduler``. For more detailed information, you can
9+
visit and learn from each library documentation.
810

9-
schedule
10-
--------
11+
Using ``schedule``
12+
------------------
13+
14+
- Install with ``pip3 install schedule``
15+
- Documentation: https://schedule.readthedocs.io
1116

1217
.. code-block:: python
1318
1419
import time
20+
1521
import schedule
1622
23+
from pyrogram import Client
24+
25+
app = Client("my_account")
26+
1727
1828
def job():
1929
app.send_message("me", "Hi!")
2030
31+
2132
schedule.every(3).seconds.do(job)
2233
2334
with app:
2435
while True:
2536
schedule.run_pending()
2637
time.sleep(1)
2738
28-
Note that schedule is not suitable for async version of pyrogram.
29-
For more information read `library <https://schedule.readthedocs.io/>`_ docs.
3039
31-
apscheduler
32-
-----------
40+
41+
Using ``apscheduler``
42+
---------------------
43+
44+
- Install with ``pip3 install apscheduler``
45+
- Documentation: https://apscheduler.readthedocs.io
3346

3447
.. code-block:: python
3548
36-
import time
3749
from apscheduler.schedulers.background import BackgroundScheduler
3850
51+
from pyrogram import Client
52+
53+
app = Client("my_account")
54+
3955
4056
def job():
4157
app.send_message("me", "Hi!")
4258
4359
4460
scheduler = BackgroundScheduler()
45-
scheduler.add_job(job, 'interval', seconds=3)
61+
scheduler.add_job(job, "interval", seconds=3)
4662
4763
scheduler.start()
4864
app.run()
4965
50-
Apscheduler supports async version of pyrogram too, here is async example:
66+
``apscheduler`` does also support async code, here's an example with
67+
`Pyrogram Asyncio <https://docs.pyrogram.org/intro/install.html#asynchronous>`_:
5168

5269
.. code-block:: python
5370
5471
from apscheduler.schedulers.asyncio import AsyncIOScheduler
5572
73+
from pyrogram import Client
74+
75+
app = Client("my_account")
76+
5677
5778
async def job():
5879
await app.send_message("me", "Hi!")
5980
6081
6182
scheduler = AsyncIOScheduler()
62-
scheduler.add_job(job, 'interval', seconds=3)
83+
scheduler.add_job(job, "interval", seconds=3)
6384
6485
scheduler.start()
6586
app.run()
6687
67-
For more information read `library <https://apscheduler.readthedocs.io/>`_ docs.

0 commit comments

Comments
 (0)