Remove context copying from run_async function

This was required with the previous implementation of Werkzeug's
locals which didn't persist across threads. However as the current
implementation uses ContextVars which do persist the context copying
is no longer required.
This commit is contained in:
pgjones 2021-05-02 20:23:08 +01:00 committed by David Lord
parent 188f4785ad
commit cb13128cf0
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8

View file

@ -819,33 +819,8 @@ def run_async(func: t.Callable[..., t.Coroutine]) -> t.Callable[..., t.Any]:
)
@wraps(func)
def outer(*args: t.Any, **kwargs: t.Any) -> t.Any:
"""This function grabs the current context for the inner function.
def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
return async_to_sync(func)(*args, **kwargs)
This is similar to the copy_current_xxx_context functions in the
ctx module, except it has an async inner.
"""
ctx = None
if _request_ctx_stack.top is not None:
ctx = _request_ctx_stack.top.copy()
@wraps(func)
async def inner(*a: t.Any, **k: t.Any) -> t.Any:
"""This restores the context before awaiting the func.
This is required as the function must be awaited within the
context. Only calling ``func`` (as per the
``copy_current_xxx_context`` functions) doesn't work as the
with block will close before the coroutine is awaited.
"""
if ctx is not None:
with ctx:
return await func(*a, **k)
else:
return await func(*a, **k)
return async_to_sync(inner)(*args, **kwargs)
outer._flask_sync_wrapper = True # type: ignore
return outer
wrapper._flask_sync_wrapper = True # type: ignore
return wrapper