<spanid="async-await"></span><h1>Using <codeclass="docutils literal notranslate"><spanclass="pre">async</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">await</span></code><aclass="headerlink"href="#using-async-and-await"title="Link to this heading">¶</a></h1>
<p><spanclass="versionmodified added">Added in version 2.0.</span></p>
</div>
</details><p>Routes, error handlers, before request, after request, and teardown
functions can all be coroutine functions if Flask is installed with the
<codeclass="docutils literal notranslate"><spanclass="pre">async</span></code> extra (<codeclass="docutils literal notranslate"><spanclass="pre">pip</span><spanclass="pre">install</span><spanclass="pre">flask[async]</span></code>). This allows views to be
defined with <codeclass="docutils literal notranslate"><spanclass="pre">async</span><spanclass="pre">def</span></code> and use <codeclass="docutils literal notranslate"><spanclass="pre">await</span></code>.</p>
<p>Pluggable class-based views also support handlers that are implemented as
coroutines. This applies to the <aclass="reference internal"href="api.html#flask.views.View.dispatch_request"title="flask.views.View.dispatch_request"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">dispatch_request()</span></code></a>
method in views that inherit from the <aclass="reference internal"href="api.html#flask.views.View"title="flask.views.View"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">flask.views.View</span></code></a> class, as
well as all the HTTP method handlers in views that inherit from the
<codeclass="docutils literal notranslate"><spanclass="pre">await</span></code> and ASGI use standard, modern Python capabilities. Deciding
whether you should use Flask, Quart, or something else is ultimately up
to understanding the specific needs of your project.</p>
</section>
<sectionid="extensions">
<h2>Extensions<aclass="headerlink"href="#extensions"title="Link to this heading">¶</a></h2>
<p>Flask extensions predating Flask’s async support do not expect async views.
If they provide decorators to add functionality to views, those will probably
not work with async views because they will not await the function or be
awaitable. Other functions they provide will not be awaitable either and
will probably be blocking if called within an async view.</p>
<p>Extension authors can support async functions by utilising the
<aclass="reference internal"href="api.html#flask.Flask.ensure_sync"title="flask.Flask.ensure_sync"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">flask.Flask.ensure_sync()</span></code></a> method. For example, if the extension
provides a view function decorator add <codeclass="docutils literal notranslate"><spanclass="pre">ensure_sync</span></code> before calling
<p>Check the changelog of the extension you want to use to see if they’ve
implemented async support, or make a feature request or PR to them.</p>
</section>
<sectionid="other-event-loops">
<h2>Other event loops<aclass="headerlink"href="#other-event-loops"title="Link to this heading">¶</a></h2>
<p>At the moment Flask only supports <aclass="reference external"href="https://docs.python.org/3/library/asyncio.html#module-asyncio"title="(in Python v3.13)"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">asyncio</span></code></a>. It’s possible to
override <aclass="reference internal"href="api.html#flask.Flask.ensure_sync"title="flask.Flask.ensure_sync"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">flask.Flask.ensure_sync()</span></code></a> to change how async functions