Skip to content

Commit

Permalink
Improve idle() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
delivrance committed Jun 20, 2022
1 parent b35810d commit 0a50520
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pyrogram/methods/utilities/idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

log = logging.getLogger(__name__)

is_idling = False

# Signal number to name
signals = {
k: v for v, k in signal.__dict__.items()
Expand Down Expand Up @@ -71,18 +69,19 @@ async def main():
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

0 comments on commit 0a50520

Please sign in to comment.