update async docs

This commit is contained in:
David Lord 2021-04-06 15:31:28 -07:00
parent 61fbae8664
commit dc3e9c0cc3
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
7 changed files with 109 additions and 65 deletions

View file

@ -1520,12 +1520,13 @@ class Flask(Scaffold):
return False
def ensure_sync(self, func):
"""Ensure that the returned function is sync and calls the async func.
"""Ensure that the function is synchronous for WSGI workers.
Plain ``def`` functions are returned as-is. ``async def``
functions are wrapped to run and wait for the response.
Override this method to change how the app runs async views.
.. versionadded:: 2.0
Override if you wish to change how asynchronous functions are
run.
"""
if iscoroutinefunction(func):
return run_async(func)

View file

@ -475,8 +475,8 @@ class Blueprint(Scaffold):
"""Ensure the function is synchronous.
Override if you would like custom async to sync behaviour in
this blueprint. Otherwise :meth:`~flask.Flask..ensure_sync` is
used.
this blueprint. Otherwise the app's
:meth:`~flask.Flask.ensure_sync` is used.
.. versionadded:: 2.0
"""

View file

@ -742,9 +742,10 @@ def run_async(func):
"Install Flask with the 'async' extra in order to use async views."
)
# Check that Werkzeug isn't using its fallback ContextVar class.
if ContextVar.__module__ == "werkzeug.local":
raise RuntimeError(
"async cannot be used with this combination of Python & Greenlet versions"
"Async cannot be used with this combination of Python & Greenlet versions."
)
@wraps(func)
@ -763,9 +764,9 @@ def run_async(func):
async def inner(*a, **k):
"""This restores the context before awaiting the func.
This is required as the func must be awaited within the
context. Simply calling func (as per the
copy_current_xxx_context functions) doesn't work as the
This is required as the function must be awaited within the
context. Only calling ``func`` (as per the
``copy_current_xxx_context`` functions) doesn't work as the
with block will close before the coroutine is awaited.
"""
if ctx is not None: