0

I can't seem to understand why CTL+C is not working.

I think it's something I don't yet understand about the way async functions work and asyncio.sleep()

However, I have found this bug report which might be related as well as people using event loops with async functions in python. Also, there are questions like run async while loop independently but they don't really answer my question or provide insight.

async def set_maintain_countdown_message(countdown, message):
    try:
        countdown_message = await app.send_message(
            message.chat.id,
            await get_formated_countdown(countdown)
            )
        await asyncio.sleep(random.randint(4, 8))
        while countdown['state'] == 'active':
            await countdown_message.edit(
                await get_formated_countdown(countdown)
            )
            await asyncio.sleep(random.randint(4, 8))

    except FloodWait as e:
        await asyncio.sleep(e.x)
Daniel Walker
  • 5,220
  • 3
  • 18
  • 39
Chris
  • 535
  • 7
  • 26

1 Answers1

2

You can implement it using custom signal handlers.

Unfortunately asyncio by default doesn't work that way, and for a major reason: If you have multiple coroutines waiting at once, which one will it cancel?

Also, I will not go into how asyncio is implemented internally, which I answered on a different question, but the interpreter isn't actually sleeping at that line of code, so a KeyboardInterrupt won't be thrown there.

Creating a custom signal handler to handle SIGINT is probably what you're looking for.

Keep in mind - if you do not use asyncio.run() to run the code, you must make sure you shut down the coroutines (cancelling everything) gracefully using loop.shutdown_asyncgens().

Bharel
  • 20,128
  • 3
  • 33
  • 62