2020-08-22 06:05:05 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2021-01-01 21:58:48 +00:00
|
|
|
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
|
2020-08-22 06:05:05 +00:00
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import logging
|
2021-04-15 10:04:43 +00:00
|
|
|
import signal
|
|
|
|
from signal import signal as signal_fn, SIGINT, SIGTERM, SIGABRT
|
2020-08-22 06:05:05 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-08-25 12:17:39 +00:00
|
|
|
is_idling = False
|
2020-08-22 06:05:05 +00:00
|
|
|
|
2021-04-15 10:04:43 +00:00
|
|
|
# Signal number to name
|
|
|
|
signals = {
|
|
|
|
k: v for v, k in signal.__dict__.items()
|
|
|
|
if v.startswith("SIG") and not v.startswith("SIG_")
|
|
|
|
}
|
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
|
|
|
|
async def 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 :meth:`~pyrogram.Client.stop` for each running client before the script ends.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
.. code-block:: python
|
|
|
|
:emphasize-lines: 13
|
|
|
|
|
|
|
|
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()
|
|
|
|
"""
|
2020-08-25 12:17:39 +00:00
|
|
|
global is_idling
|
2020-08-22 06:05:05 +00:00
|
|
|
|
2021-04-15 10:04:43 +00:00
|
|
|
def signal_handler(signum, __):
|
2020-08-25 12:17:39 +00:00
|
|
|
global is_idling
|
2020-08-22 06:05:05 +00:00
|
|
|
|
2021-04-15 10:04:43 +00:00
|
|
|
logging.info(f"Stop signal received ({signals[signum]}). Exiting...")
|
2020-08-25 12:17:39 +00:00
|
|
|
is_idling = False
|
2020-08-22 06:05:05 +00:00
|
|
|
|
2020-08-25 12:17:39 +00:00
|
|
|
for s in (SIGINT, SIGTERM, SIGABRT):
|
2021-04-15 10:04:43 +00:00
|
|
|
signal_fn(s, signal_handler)
|
2020-08-22 06:05:05 +00:00
|
|
|
|
2020-08-25 12:17:39 +00:00
|
|
|
is_idling = True
|
|
|
|
|
|
|
|
while is_idling:
|
|
|
|
await asyncio.sleep(1)
|