-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
incompatible with flask 2 and async #158
Comments
Same issue when using async. I am getting this error message.
Edit: Did work (sometimes) if wrap it inside a render_template(), but for other responses like json, instead of ignore it like it should, it tries to run it anyway if using async. |
The flask-debugtoolbar has its own def dispatch_request(self) -> ResponseReturnValue:
"""Does the request dispatching. Matches the URL and returns the
return value of the view or error handler. This does not have to
be a response object. In order to convert the return value to a
proper response object, call :func:`make_response`.
.. versionchanged:: 0.7
This no longer does the exception handling, this code was
moved to the new :meth:`full_dispatch_request`.
"""
req = _request_ctx_stack.top.request
if req.routing_exception is not None:
self.raise_routing_exception(req)
rule = req.url_rule
# if we provide automatic options for this URL and the
# request came with the OPTIONS method, reply automatically
if (
getattr(rule, "provide_automatic_options", False)
and req.method == "OPTIONS"
):
return self.make_default_options_response()
# otherwise dispatch to the handler for that endpoint
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) I'm not an expert on this but I guess calling |
Happy to merge a PR if you or anyone else wants to dig into it. On the surface, it looks relatively straightforward--port the updated version from My current work is unrelated to Flask, so won't have time to look into it myself anytime soon. |
I've quickly tried the following hack/changes. Seems to work: 1️⃣ override from asyncio import iscoroutinefunction
from asgiref.sync import async_to_sync
from flask_debugtoolbar import DebugToolbarExtension
class MyDebugToolbarExtension(DebugToolbarExtension):
def process_view(self, app, view_func, view_kwargs):
processed_view = super().process_view(app, view_func, view_kwargs)
if iscoroutinefunction(processed_view):
processed_view = async_to_sync(processed_view)
return processed_view 2️⃣ Enable sql query recording of an async engine: from flask_sqlalchemy import record_queries
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(db_url)
record_queries._listen(engine.sync_engine) Note that this is calling a "private" function, Disclaimer: I've only just discovered flask-sqlalchemy and flask-debugtoolbar, so there may be better ways of doing this! |
It seems that flask-debugtoolbar is incompatible with flask 2 async endpoints.
Repro:
Run this flask project (app.py) and hit
/data
to trigger an async call - which works.Then uncomment the line enabling the
DebugToolbarExtension
and we get an exception.The text was updated successfully, but these errors were encountered: