Issue
I am attempting to run Flask in an async manner on python 3.6.10, but keep getting:
RuntimeError: Async cannot be used with this combination of Python and Greenlet versions.
I have written the following simple script to test the flask async functionality:
from flask import Flask
import asyncio
app = Flask(__name__)
async def load_user_from_database():
"""Mimics a long-running operation to load a user from an external database."""
app.logger.info('Loading user from database...')
await asyncio.sleep(1)
@app.before_request
async def add_drink():
await load_user_from_database()
Upgrading to python 3.9.2 seems to solve the issue, but for my application, I am forced to use python 3.6.10 and therefore welcome any suggestion to get it to work on the latter python version.
P.S The following are the requirements.txt for the simple app:
aiohttp==3.7.4.post0
asgiref==3.3.4
async-timeout==3.0.1
attrs==20.3.0
certifi==2020.12.5
chardet==4.0.0
click==8.0.0rc1
Flask==2.0.0
idna==2.10
iniconfig==1.1.1
itsdangerous==2.0.0
Jinja2==3.0.0
MarkupSafe==2.0.0rc2
multidict==5.1.0
packaging==20.9
pluggy==0.13.1
py==1.10.0
pyparsing==2.4.7
pytest==6.2.4
pytest-asyncio==0.15.1
requests==2.25.1
toml==0.10.2
typing-extensions==3.10.0.0
urllib3==1.26.4
Werkzeug==2.0.0
yarl==1.6.3
Solution
Flask.async_to_sync()
in Flask 2.0 requires ContextVar
. Minimal Python requirement for async
support in Flask is 3.7 (when contextvars
are introduced) if you are not using Greenlet. If you're using Greenlet, it also depends on the Greenlet version.
Answered By - Simba
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.