Polars use nest_asyncio #17334
Labels
bug
Something isn't working
needs triage
Awaiting prioritization by a maintainer
python
Related to Python Polars
Checks
Reproducible example
No polars example at hand since I only realized polars used nest_asyncio from the changelog. The general problem looks like that:
Log output
Issue description
Polars currently relies on nest_asyncio:
polars/py-polars/polars/io/database/_utils.py
Line 39 in f73937a
I would suggest to avoid doing so, as
nest_asyncio
only works with event loop implementations shipped with the standard library (as per its documentation). On top of that, we observed that failednest_asyncio.apply()
calls were leaving behind some broken state that would force to restart the process [1].The library is currently not maintained anymore (github repo is archived and the original author and maintainer has sadly left us), so there is little hope of these issues fixed shortly.
We faced the aforementioned issues when running under uvloop (FastAPI server with uvicorn), which lead us to implement an equivalent based on greenlets (see
devlib.utils.asyn.run()
for an equivalent ofasyncio.run()
that nests). greenback does more-or-less the same thing but seemed to be using unsafe code (access of stdlib C structs using ctypes from pure-Python) [2].[1] I can't remember the details, something related to the event loop policy staying patched to modify event loops to use nest_asyncio's equivalent to the original
asyncio.run()
)[2] AFAIR greenback uses ctypes to patch the current running
asyncio.Task
inawait greenback.ensure_portal()
, which we found unnecessary in devlib. Instead we use loop.set_task_factory to interpose our coroutine shim in the top-level . If we happen to run in an non-instrumented asyncio.Task, we just delegate the call to another thread with another event loop as a fallback.Expected behavior
Polars should work with any asyncio event loop implementation.
Installed versions
N/A
The text was updated successfully, but these errors were encountered: