2

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
1
  • Opting for a different library such as fastAPI might indeed be better for the future. But given that the whole codebase is written in Flask, and that only one API is required to become asynchronous I find it easier to stick with Flask May 26, 2021 at 7:50

1 Answer 1

2

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.

2
  • Very insightful! So I take it that it is indeed impossible to get an async flask to work with python 3.6.10 May 28, 2021 at 13:31
  • 1
    @MarcDjandji Yes. If you're not using async in Flask, the minimal Python requirement is 3.6. But for Flask with async support, the minimal Python requirement is 3.7. If you're using async greenlet, it also depends on the version of greenlet.
    – Simba
    May 30, 2021 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.