Library seems incompatible with asyncpg
#27
Replies: 3 comments
-
|
It seems like this might be an interaction with asgiref or SQLAlchemy's use of greenlet. I don't believe Flask-SQLAlchemy-Lite has any control over this, or is doing anything to affect it. Flask uses asgiref to run coroutines in the background, since it is a sync WSGI framework. I don't personally use async, so I can't say why this isn't working beyond that. I'm happy to review a PR if you can fix it here, but it's likely a problem somewhere in the sqlalchemy/greenlet/asyncpg/asgiref interactions. |
Beta Was this translation helpful? Give feedback.
-
|
I think this is due to Flask's use of asgiref, since every async callback runs it its own loop, but certain objects like asyncpg hold a reference to the original loop. Flask would need some way to run an entire context in a single loop, which is a huge task that would require reimplementing our async handling. You should see/request if it's possible for asyncpg/sqlalchemy/greenlet to not hold a reference to the loop. That's much more likely to happen on any reasonable timescale than rewriting Flask's async handling. Alternatively, use Quart, ASGI will be much better at this than WSGI. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the answer. I suspected something like this was happening but didn't know enough of where to start looking to really be able to sort it out. Since this seems like "not a bug" I wouldn't object to it being moved to a discussion rather than an issue since others might find it helpful as I've seen similar issues with Flask / SqlA while trying to debug this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
A slightly modified version of the code from the README cannot make DB queries in a request handler.
Code:
Python version 3.13.5
pyproject.tomlcontents:`uv.lock` contents:
Beta Was this translation helpful? Give feedback.
All reactions