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.
This commit is contained in:
pgjones 2021-03-24 20:47:55 +00:00 committed by David Lord
parent 47e0f2aab8
commit d65f574bff
5 changed files with 169 additions and 32 deletions

View file

@ -4,7 +4,6 @@ import pkgutil
import sys
from collections import defaultdict
from functools import update_wrapper
from inspect import iscoroutinefunction
from jinja2 import FileSystemLoader
from werkzeug.exceptions import default_exceptions
@ -13,7 +12,6 @@ from werkzeug.exceptions import HTTPException
from .cli import AppGroup
from .globals import current_app
from .helpers import locked_cached_property
from .helpers import run_async
from .helpers import send_from_directory
from .templating import _default_template_ctx_processor
@ -687,17 +685,7 @@ class Scaffold:
return exc_class, None
def ensure_sync(self, func):
"""Ensure that the returned function is sync and calls the async func.
.. versionadded:: 2.0
Override if you wish to change how asynchronous functions are
run.
"""
if iscoroutinefunction(func):
return run_async(func)
else:
return func
raise NotImplementedError()
def _endpoint_from_view_func(view_func):