From 0a50520fc94ef53a55f6d36714102f934f891b37 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 20 Jun 2022 09:51:16 +0200 Subject: [PATCH] Improve idle() implementation --- pyrogram/methods/utilities/idle.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pyrogram/methods/utilities/idle.py b/pyrogram/methods/utilities/idle.py index bcd685e0..00db22a2 100644 --- a/pyrogram/methods/utilities/idle.py +++ b/pyrogram/methods/utilities/idle.py @@ -23,8 +23,6 @@ from signal import signal as signal_fn, SIGINT, SIGTERM, SIGABRT log = logging.getLogger(__name__) -is_idling = False - # Signal number to name signals = { k: v for v, k in signal.__dict__.items() @@ -71,18 +69,19 @@ async def idle(): asyncio.run(main()) """ - global is_idling + task = None def signal_handler(signum, __): - global is_idling - logging.info(f"Stop signal received ({signals[signum]}). Exiting...") - is_idling = False + task.cancel() for s in (SIGINT, SIGTERM, SIGABRT): signal_fn(s, signal_handler) - is_idling = True + while True: + task = asyncio.create_task(asyncio.sleep(600)) - while is_idling: - await asyncio.sleep(1) + try: + await task + except asyncio.CancelledError: + break