flask icon indicating copy to clipboard operation
flask copied to clipboard

asyncio is not compatible with gevent

Open awoimbee opened this issue 3 months ago • 6 comments

See #5256

async-await: https://flask.palletsprojects.com/en/stable/async-await/

Using async with greenlet When using gevent or eventlet to serve an application or patch the runtime, greenlet>=1.0 is required. When using PyPy, PyPy>=7.3.7 is required.

Flask uses asgiref, which is not compatible with gevent: https://github.com/django/asgiref/issues/443

BTW: ASGI: https://flask.palletsprojects.com/en/stable/deploying/asgi/

The asgiref WsgiToAsgi adapter is recommended as it integrates with the event loop used for Flask’s Using async and await support.

This makes it seem like the event loop integration means a single worker could process multiple requests concurently, which it can't, the worker will block on await.


I'm not the only one that found this the hard way: Flask, Gunicorn, Gevent and Asyncio Don't Mix

awoimbee avatar Sep 16 '25 12:09 awoimbee

~~Oh no, it actually works with uwsgi.~~ It doesn't work either with uwsgi

RuntimeError: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.

awoimbee avatar Sep 16 '25 15:09 awoimbee

It would be helpful if you showed a minimal example of what you are using using that causes issues.

davidism avatar Sep 19 '25 14:09 davidism

Look at the linked flask-gunicorn-gevent-asyncio repo. Also check the attached reproducer in https://github.com/benoitc/gunicorn/issues/3070.

awoimbee avatar Sep 19 '25 14:09 awoimbee

Please include the example and relevant details here. A whole Docker container is not a minimal example. I also can't tell if you're talking about gevent alone, or uwsgi, or gunicorn, or something else. Please be specific about the exact issue you are reporting.

davidism avatar Sep 19 '25 14:09 davidism

Please include the example [...] A whole Docker container is not a minimal example.

The dockerfile is completely optional, you just need python and pip to run the example in flask-gunicorn-gevent-asyncio.

Here is the zip From the gunicorn issue: https://github.com/benoitc/gunicorn/files/12614245/reproduction.zip It contains:

app.py
__init__.py
lots-of-curls.sh
requirements.txt

I also can't tell if you're talking about gevent alone, or uwsgi, or gunicorn, or something else.

I'm talking about gevent + asgiref (flask's async-await implementation).

asgiref does not support gevent (ref), so flask[async] cannot support gevent. Which is fine, but then I have an issue with the docs having a "Using async with greenlet" section.

awoimbee avatar Sep 19 '25 14:09 awoimbee

@pgjones could you take a look at this if you have a chance?

davidism avatar Sep 19 '25 23:09 davidism