Commit Graph

12 Commits

Author SHA1 Message Date
David Lord 6650764e97
remove previously deprecated code 2023-02-23 08:35:16 -08:00
David Lord 96c97dec09
deprecate before_first_request 2022-06-06 10:04:05 -07:00
David Lord e609dddd60
drop Python 3.6 2021-11-11 16:11:43 -08:00
pre-commit-ci[bot] 5205cd4ea9 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2021-06-01 18:08:19 +00:00
Miguel Grinberg 270eb2df2a Support View and MethodView instances with async handlers 2021-06-01 18:08:19 +00:00
pgjones 2889da67cb
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.
2021-05-03 06:18:41 -07:00
pgjones 7f87f3dd93
Simplify the async handling code
Firstly `run_sync` was a misleading name as it didn't run anything,
instead I think `async_to_sync` is much clearer as it converts a
coroutine function to a function. (Name stolen from asgiref).

Secondly trying to run the ensure_sync during registration made the
code more complex and brittle, e.g. the _flask_async_wrapper
usage. This was done to pay any setup costs during registration rather
than runtime, however this only saved a iscoroutne check. It allows
the weirdness of the Blueprint and Scaffold ensure_sync methods to be
removed.

Switching to runtime ensure_sync usage provides a method for
extensions to also support async, as now documented.
2021-05-03 06:15:39 -07:00
pgjones 5c6a0f0c12 Fix wrapped view function comparison
Wrapped functions are not comparable, see
https://bugs.python.org/issue3564, therefore a marker is used to note
when the function has been sync wrapped to allow comparison with the
wrapped function instead.

This ensures that multiple route decorators work without raising
exceptions i.e.,

    @app.route("/")
    @app.route("/a")
    async def index():
        ...

works.
2021-04-16 12:34:51 +01:00
David Lord 61fbae8664
skip async tests if asgiref isn't installed 2021-04-06 15:33:06 -07:00
pgjones 00f5a3e55c
Alter ensure_sync implementation to support extensions
This allows extensions to override the Flask.ensure_sync method and
have the change apply to blueprints as well. Without this change it is
possible for differing blueprints to have differing ensure_sync
approaches depending on the extension used - which would likely result
in event-loop blocking issues.

This also allows blueprints to have a custom ensure_sync, although
this is a by product rather than an expected use case.
2021-04-06 15:33:06 -07:00
pgjones c6c6408c3f
Raise a runtime error if run_async is called without real ContextVars
Werkzeug offers a ContextVar replacement for Python < 3.7, however it
doesn't work across asyncio tasks, hence it makes sense to error out
rather than find there are odd bugs.

Note the docs build requires the latest (dev) Werkzeug due to this
change (to import ContextVar from werkzeug.local).
2021-04-06 09:35:10 -07:00
pgjones 6979265fa6
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.
2021-04-06 09:35:10 -07:00