Skip to content

Commit 385ab22

Browse files
committed
Rework Client.idle()
idle() is now static and doesn't stop the client anymore
1 parent fed8cbf commit 385ab22

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

pyrogram/client/client.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -394,37 +394,60 @@ def restart(self):
394394
self.stop()
395395
self.start()
396396

397-
def idle(self, stop_signals: tuple = (SIGINT, SIGTERM, SIGABRT)):
397+
@staticmethod
398+
def idle(stop_signals: tuple = (SIGINT, SIGTERM, SIGABRT)):
398399
"""Block the main script execution until a signal is received.
399400
400-
Once the signal is received (e.g.: from CTRL+C), the client will automatically stop and the main script will
401-
continue its execution.
401+
This static method will run an infinite loop in order to block the main script execution and prevent it from
402+
exiting while having client(s) that are still running in the background.
402403
403-
This is used after starting one or more clients and is useful for event-driven applications only, that are,
404-
applications which react upon incoming Telegram updates through handlers, rather than executing a set of methods
405-
sequentially.
404+
It is useful for event-driven application only, that are, applications which react upon incoming Telegram
405+
updates through handlers, rather than executing a set of methods sequentially.
406406
407-
The way Pyrogram works, will keep your handlers in a pool of workers, which are executed concurrently outside
408-
the main script; calling idle() will ensure the client(s) will be kept alive by not letting the main script to
409-
end, until you decide to quit.
407+
The way Pyrogram works, it will keep your handlers in a pool of worker threads, which are executed concurrently
408+
outside the main thread; calling idle() will ensure the client(s) will be kept alive by not letting the main
409+
script to end, until you decide to quit.
410+
411+
Once a signal is received (e.g.: from CTRL+C) the inner infinite loop will break and your main script will
412+
continue. Don't forget to call :meth:`~Client.stop` for each running client before the script ends.
410413
411414
Parameters:
412415
stop_signals (``tuple``, *optional*):
413416
Iterable containing signals the signal handler will listen to.
414-
Defaults to (SIGINT, SIGTERM, SIGABRT).
415-
"""
417+
Defaults to *(SIGINT, SIGTERM, SIGABRT)*.
418+
419+
Example:
420+
.. code-block:: python
421+
:emphasize-lines: 13
422+
423+
from pyrogram import Client
424+
425+
app1 = Client("account1")
426+
app2 = Client("account2")
427+
app3 = Client("account3")
428+
429+
... # Set handlers up
416430
417-
# TODO: Maybe make this method static and don't automatically stop
431+
app1.start()
432+
app2.start()
433+
app3.start()
434+
435+
Client.idle()
436+
437+
app1.stop()
438+
app2.stop()
439+
app3.stop()
440+
"""
418441

419442
def signal_handler(*args):
420-
self.is_idle = False
443+
Client.is_idling = False
421444

422445
for s in stop_signals:
423446
signal(s, signal_handler)
424447

425-
self.is_idle = True
448+
Client.is_idling = True
426449

427-
while self.is_idle:
450+
while Client.is_idling:
428451
time.sleep(1)
429452

430453
self.stop()

pyrogram/client/ext/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class StopTransmission(StopIteration):
8989

9090
mime_types_to_extensions[mime_type] = " ".join(extensions)
9191

92+
is_idling = False
93+
9294
def __init__(self):
9395
self.storage = None
9496

@@ -102,7 +104,6 @@ def __init__(self):
102104
self.media_sessions_lock = Lock()
103105

104106
self.is_started = None
105-
self.is_idle = None
106107

107108
self.takeout_id = None
108109

0 commit comments

Comments
 (0)