[Async-sig] new library: sniffio – Sniff out which async library your code is running under

Nathaniel Smith njs at pobox.com
Fri Aug 17 15:38:52 EDT 2018


On Fri, Aug 17, 2018, 09:09 Alex Grönholm <alex.gronholm at nextday.fi> wrote:

> This was my approach:
>
> def _detect_running_asynclib() -> str:
>     if 'trio' in sys.modules:
>         from trio.hazmat import current_trio_token
>         try:
>             current_trio_token()
>         except RuntimeError:
>             pass
>         else:
>             return 'trio'
>
>     if 'curio' in sys.modules:
>         from curio.meta import curio_running
>         if curio_running():
>             return 'curio'
>
>     if 'asyncio' in sys.modules:
>         from .backends.asyncio import get_running_loop
>         if get_running_loop() is not None:
>             return 'asyncio'
>
>     raise LookupError('Cannot find any running async event loop')
>
>
> Is there something wrong with this?
>

If you're using trio-asyncio, then you can have both trio-flavored
coroutines and asyncio-flavored coroutines running in the same thread. And
in particular, the trio and asyncio tests you do above will both return
true at the same time, even though at any given moment only you can only
'await' one kind of async function or the other.

Twisted running on the asyncio reactor has a similar situation.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20180817/f3522bd9/attachment.html>


More information about the Async-sig mailing list