idle()¶
-
pyrogram.idle()¶ Block the main script execution until a signal is received.
This function will run indefinitely in order to block the main script execution and prevent it from exiting while having client(s) that are still running in the background.
It is useful for event-driven application only, that are, applications which react upon incoming Telegram updates through handlers, rather than executing a set of methods sequentially.
The way Pyrogram works, it will keep your handlers in a pool of worker threads, which are executed concurrently outside the main thread; calling idle() will ensure the client(s) will be kept alive by not letting the main script to end, until you decide to quit.
Once a signal is received (e.g.: from CTRL+C) the function will terminate and your main script will continue. Don’t forget to call
stop()for each running client before the script ends.Example
from pyrogram import Client, idle app1 = Client("account1") app2 = Client("account2") app3 = Client("account3") ... # Set handlers up app1.start() app2.start() app3.start() idle() app1.stop() app2.stop() app3.stop()