python-trio / sniffio

Sniff out which async library your code is running under

Home Page:https://sniffio.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for synchronous event loops

tzickel opened this issue · comments

The same principle that exists in the asynchronous world, exists in the synchronous world as well.

I've been bitten enough by gevent's monkey patching, that I think that if library writers had an option to detect a code is running via a greenlet or normal (threaded?), it could create code that automatically knows which primitives to use.

@jamadden This could make libraries like this (which is the basis of httpx) auto detect and use gevent as well (without monkey patching) ?
https://github.com/encode/httpcore/tree/master/httpcore/_backends

I just don't understand how this would make the code manifest the necessary await etc. statements. Enlighten us please?

there's also the case for twisted in greenlet or asyncio in greenlet (and them both), eg running async sqlalchemy on pytest-twisted

I do not understand how gevent is related to async/await. As said in the OP, I would like the option for sniffio to be able to report synchronous event loops such as running inside a greenlet. Then when writing for example a sync library like redis-py it could query if it's running under a greenlet and do different code instead of using regular I/O (thus not requiring monkey patching).

Ah, I got confused. Nevermind!