forked from orbit-oss/flask
Add async support
This allows for async functions to be passed to the Flask class
instance, for example as a view function,
@app.route("/")
async def index():
return "Async hello"
this comes with a cost though of poorer performance than using the
sync equivalent.
asgiref is the standard way to run async code within a sync context,
and is used in Django making it a safe and sane choice for this.
This commit is contained in:
parent
85b8fab268
commit
6979265fa6
11 changed files with 165 additions and 9 deletions
33
tests/test_async.py
Normal file
33
tests/test_async.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from flask import abort
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
|
||||
|
||||
@pytest.fixture(name="async_app")
|
||||
def _async_app():
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
async def index():
|
||||
await asyncio.sleep(0)
|
||||
return request.method
|
||||
|
||||
@app.route("/error")
|
||||
async def error():
|
||||
abort(412)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def test_async_request_context(async_app):
|
||||
test_client = async_app.test_client()
|
||||
response = test_client.get("/")
|
||||
assert b"GET" in response.get_data()
|
||||
response = test_client.post("/")
|
||||
assert b"POST" in response.get_data()
|
||||
response = test_client.get("/error")
|
||||
assert response.status_code == 412
|
||||
Loading…
Add table
Add a link
Reference in a new issue