where
This is on Linux, Python 3.5.1.
what
I'm developing a monitor process with asyncio
, whose tasks at various places await
on asyncio.sleep
calls of various durations.
There are points in time when I would like to be able to interrupt all said asyncio.sleep
calls and let all tasks proceed normally, but I can't find how to do that. An example is for graceful shutdown of the monitor process.
how (failed assumption)
I thought that I could send an ALRM signal to that effect, but the process dies. I tried catching the ALRM signal with:
def sigalrm_sent(signum, frame):
tse.logger.info("got SIGALRM")
signal.signal(signal.SIGALRM, sigalrm_sent)
Then I get the log line about catching SIGALRM, but the asyncio.sleep
calls are not interrupted.
how (kludge)
At this point, I replaced all asyncio.sleep
calls with calls to this coroutine:
async def interruptible_sleep(seconds):
while seconds > 0 and not tse.stop_requested:
duration = min(seconds, tse.TIME_QUANTUM)
await asyncio.sleep(duration)
seconds -= duration
So I only have to pick a TIME_QUANTUM
that is not too small and not too large either.
but
Is there a way to interrupt all running asyncio.sleep
calls and I am missing it?