Skip to main content

Flask running on asyncio.

Project description

aioflask

Build status codecov

Flask 2.x running on asyncio!

Is there a purpose for this, now that Flask 2.0 is out with support for async views? Yes! Flask's own support for async handlers is very limited, as the application still runs inside a WSGI web server, which severely limits scalability. With aioflask you get a true ASGI application, running in a 100% async environment.

WARNING: This is an experiment at this point. Not at all production ready!

Quick start

To use async view functions and other handlers, use the aioflask package instead of flask.

The aioflask.Flask class is a subclass of flask.Flask that changes a few minor things to help the application run properly under the asyncio loop. In particular, it overrides the following aspects of the application instance:

  • The route, before_request, before_first_request, after_request, teardown_request, teardown_appcontext, errorhandler and cli.command decorators accept coroutines as well as regular functions. The handlers all run inside an asyncio loop, so when using regular functions, care must be taken to not block.
  • The WSGI callable entry point is replaced with an ASGI equivalent.
  • The run() method uses uvicorn as web server.

There are also changes outside of the Flask class:

  • The flask aiorun command starts an ASGI application using the uvicorn web server.
  • The render_template() and render_template_string() functions are asynchronous and must be awaited.
  • The context managers for the Flask application and request contexts are async.
  • The test client and test CLI runner use coroutines.

Example

import asyncio
from aioflask import Flask, render_template

app = Flask(__name__)

@app.route('/')
async def index():
    await asyncio.sleep(1)
    return await render_template('index.html')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioflask-0.4.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

aioflask-0.4.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file aioflask-0.4.0.tar.gz.

File metadata

  • Download URL: aioflask-0.4.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.3

File hashes

Hashes for aioflask-0.4.0.tar.gz
Algorithm Hash digest
SHA256 96403709fff88351c606667fce1a5a66bdef2d634a04b358713c8304216db68f
MD5 84d13d3c60a8066c2e718cab871d46df
BLAKE2b-256 9d07ac0afe199fde1764927ce68f254e9fe3c174c4bb689f74b8a40974c5b4cc

See more details on using hashes here.

File details

Details for the file aioflask-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: aioflask-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.3

File hashes

Hashes for aioflask-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f0c2327f973a853a37b1506039e74bc532d58a009427e66bc37bf1e7bffbff2
MD5 c2d88ce9218186b78b77f504d5237086
BLAKE2b-256 b6fc961dc7419b4095eedb87116dc7b4c4542cd5fa14403dc8b85ef3956d9383

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page