[Async-sig] Cancelling a coroutine from a signal handler?

Mark E. Haase mehaase at gmail.com
Wed Apr 25 09:33:42 EDT 2018

On Tue, Apr 24, 2018 at 9:54 PM, Nathaniel Smith <njs at pobox.com> wrote:
> The simplest solution would be to use asyncio's native signal handler
> support instead of the signal module:
> https://docs.python.org/3/library/asyncio-eventloop.html#unix-signals

Ahh, wow, I don't know how I missed this. I've been obsessing over
coroutines and event loops for hours, now I realize that I misunderstood
the voodoo in the signal module. Thank you for pointing me in this

Alternatively, you can define a handler using signal.signal, and then
> arrange to re-enter the asyncio main loop yourself before calling
> Task.cancel. I believe that the only guaranteed-to-be-safe way to do
> this is:

This is also an interesting approach that I will experiment with. I guess
this solves problem #1 (works on Windows) but not #2 (task stuck in loop),
right? (The latter is a feature of all cooperative multitasking systems,

Great blog post today! I really enjoy your writing style and Trio is really

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20180425/3d7d0a52/attachment.html>

More information about the Async-sig mailing list