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:
pgjones 2020-07-06 20:54:26 +01:00 committed by David Lord
parent 8fa1fdfc96
commit c9f774d650
11 changed files with 165 additions and 9 deletions

View file

@ -171,6 +171,18 @@ Also see the :doc:`/becomingbig` section of the documentation for some
inspiration for larger applications based on Flask.
Async-await and ASGI support
----------------------------
Flask supports ``async`` coroutines for view functions, and certain
others by executing the coroutine on a seperate thread instead of
utilising an event loop on the main thread as an async first (ASGI)
frameworks would. This is necessary for Flask to remain backwards
compatibility with extensions and code built before ``async`` was
introduced into Python. This compromise introduces a performance cost
compared with the ASGI frameworks, due to the overhead of the threads.
What Flask is, What Flask is Not
--------------------------------