Skip to content
Myr

Remove the async helper method

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by pgjones · committed by David Lord · parent 7f87f3d · Browse files · 3 files+34 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+34 added−28 removed

  • tests:+2 added−2 removed
  • other:+32 added−26 removed

3 files+34 added−28 removed

modified:src/flask/app.py+32 added-2 removed
14 unmodified lines
15from werkzeug.exceptions import BadRequest16from werkzeug.exceptions import BadRequestKeyError17from werkzeug.exceptions import HTTPException18from werkzeug.exceptions import InternalServerError19from werkzeug.local import ContextVar20from werkzeug.routing import BuildError21from werkzeug.routing import Map22from werkzeug.routing import MapAdapter23from werkzeug.routing import RequestRedirect
11 unmodified lines
35from .globals import _request_ctx_stack36from .globals import g37from .globals import request38from .globals import session38from .helpers import async_to_sync39from .helpers import get_debug_flag40from .helpers import get_env41from .helpers import get_flashed_messages42from .helpers import get_load_dotenv
1535 unmodified lines
15781579 .. versionadded:: 2.01580 """1581 if iscoroutinefunction(func):1582 return async_to_sync(func)1582 return self.async_to_sync(func)15831584 return func15851586 def async_to_sync(1587 self, func: t.Callable[..., t.Coroutine]1588 ) -> t.Callable[..., t.Any]:1589 """Return a sync function that will run the coroutine function.15901591 .. code-block:: python15921593 result = app.async_to_sync(func)(*args, **kwargs)15941595 Override this method to change how the app converts async code1596 to be synchronously callable.15971598 .. versionadded:: 2.01599 """1600 try:1601 from asgiref.sync import async_to_sync as asgiref_async_to_sync1602 except ImportError:1603 raise RuntimeError(1604 "Install Flask with the 'async' extra in order to use async views."1605 )16061607 # Check that Werkzeug isn't using its fallback ContextVar class.1608 if ContextVar.__module__ == "werkzeug.local":1609 raise RuntimeError(1610 "Async cannot be used with this combination of Python "1611 "and Greenlet versions."1612 )16131614 return asgiref_async_to_sync(func)16151616 def make_response(self, rv: ResponseReturnValue) -> Response:1617 """Convert the return value from a view function to an instance of1618 :attr:`response_class`.1619
456 unmodified lines
modified:src/flask/helpers.py-24 removed
8 unmodified lines
9from threading import RLock1011import werkzeug.utils12from werkzeug.exceptions import NotFound13from werkzeug.local import ContextVar13from werkzeug.routing import BuildError14from werkzeug.urls import url_quote1516from .globals import _app_ctx_stack
781 unmodified lines
798 else:799 return True800801 return False803804805def async_to_sync(func: t.Callable[..., t.Coroutine]) -> t.Callable[..., t.Any]:806 """Return a sync function that will run the coroutine function *func*.807808 This can be used as so809810 result = async_to_async(func)(*args, **kwargs)811 """812 try:813 from asgiref.sync import async_to_sync as asgiref_async_to_sync814 except ImportError:815 raise RuntimeError(816 "Install Flask with the 'async' extra in order to use async views."817 )818819 # Check that Werkzeug isn't using its fallback ContextVar class.820 if ContextVar.__module__ == "werkzeug.local":821 raise RuntimeError(822 "Async cannot be used with this combination of Python & Greenlet versions."823 )824825 return asgiref_async_to_sync(func)
modified:tests/test_async.py+2 added-2 removed
4 unmodified lines
56from flask import Blueprint7from flask import Flask8from flask import request9from flask.helpers import async_to_sync910pytest.importorskip("asgiref")1112
121 unmodified lines
134135136@pytest.mark.skipif(sys.version_info >= (3, 7), reason="should only raise Python < 3.7")137def test_async_runtime_error():138 app = Flask(__name__)139 with pytest.raises(RuntimeError):140 async_to_sync(None)140 app.async_to_sync(None)