-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
mis-detects curio as asyncio when running in curio in asyncio.to_thread (py3.9) #20
Comments
this also happens when running curio inside an asyncio coro import curio
import asyncio
import sniffio
async def run_in_curio():
return sniffio.current_async_library()
async def run_curio():
sniffio.current_async_library()
return curio.run(run_in_curio)
print(asyncio.run(run_curio())) # prints asyncio - should print curio |
and the other way around, running asyncio inside a curio coro: import curio
import asyncio
import sniffio
async def run_in_asyncio():
return sniffio.current_async_library()
async def run_asyncio():
return asyncio.run(run_in_asyncio())
print(curio.run(run_asyncio)) # prints asyncio - should print curio |
Calling The top-level problem with As we discussed a bit on gitter, I'm no longer convinced that Probably we should just use a thread-local instead of a cvar. It'll make sniffio slightly slower on non-cooperating libraries like asyncio/curio, but I don't see any way to avoid that. Something like: class CurrentLibHolder(threading.local):
value = None
_current_lib_holder = CurrentLibHolder()
def set_current_lib(new_lib):
old_lib = _current_lib_holder.value
def restore():
_current_lib_holder.value = old_lib
_current_lib_holder.value = new_lib
return restore
def get_current_lib():
lib = _current_lib_holder.value
if lib is not None:
return lib
if asyncio.current_task() is not None:
return "asyncio"
... |
You have to walk the stack and see which library is calling g.send |
We're not walking the stack just to fix an issue with code that no-one should even be writing in the first place :-) |
Turns out it's totally fixable because asyncio and curio both communicate via sys.set_asyncgen_hooks |
The text was updated successfully, but these errors were encountered: