Remove the async helper method

It is better to encourage users to utilise the app ensure_sync method
(or the newely added async_to_sync method) so that any extensions that
alter these methods take affect throughout the users code.

With the helper method users code fix parts of their code to the
asgiref async_to_sync ignoring any extension changes.
This commit is contained in:
pgjones 2021-05-03 10:59:28 +01:00 committed by David Lord
parent 7f87f3dd93
commit 2889da67cb
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 34 additions and 28 deletions

View file

@ -10,7 +10,6 @@ from threading import RLock
import werkzeug.utils
from werkzeug.exceptions import NotFound
from werkzeug.local import ContextVar
from werkzeug.routing import BuildError
from werkzeug.urls import url_quote
@ -800,26 +799,3 @@ def is_ip(value: str) -> bool:
return True
return False
def async_to_sync(func: t.Callable[..., t.Coroutine]) -> t.Callable[..., t.Any]:
"""Return a sync function that will run the coroutine function *func*.
This can be used as so
result = async_to_async(func)(*args, **kwargs)
"""
try:
from asgiref.sync import async_to_sync as asgiref_async_to_sync
except ImportError:
raise RuntimeError(
"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."
)
return asgiref_async_to_sync(func)