Skip to content
Myr

Remove context copying from run_async function

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

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.

authored by pgjones · committed by David Lord · parent 188f478 · Browse files · 1 file+4 added−29 removed
This commit

State

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

Diff stats

1 file+4 added−29 removed

  • other:+4 added−29 removed

1 file+4 added−29 removed

modified:src/flask/helpers.py+4 added-29 removed
817 unmodified lines
818 "Async cannot be used with this combination of Python & Greenlet versions."819 )820821 @wraps(func)822 def outer(*args: t.Any, **kwargs: t.Any) -> t.Any:823 """This function grabs the current context for the inner function.822 def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:823 return async_to_sync(func)(*args, **kwargs)824825 This is similar to the copy_current_xxx_context functions in the826 ctx module, except it has an async inner.827 """828 ctx = None829830 if _request_ctx_stack.top is not None:831 ctx = _request_ctx_stack.top.copy()832833 @wraps(func)834 async def inner(*a: t.Any, **k: t.Any) -> t.Any:835 """This restores the context before awaiting the func.836837 This is required as the function must be awaited within the838 context. Only calling ``func`` (as per the839 ``copy_current_xxx_context`` functions) doesn't work as the840 with block will close before the coroutine is awaited.841 """842 if ctx is not None:843 with ctx:844 return await func(*a, **k)845 else:846 return await func(*a, **k)847848 return async_to_sync(inner)(*args, **kwargs)849850 outer._flask_sync_wrapper = True # type: ignore851 return outer825 wrapper._flask_sync_wrapper = True # type: ignore826 return wrapper