copy_current_request_context() — flask Function Reference
Architecture documentation for the copy_current_request_context() function in ctx.py from the flask codebase.
Entity Profile
Dependency Diagram
graph TD f7f0b202_038e_4088_d0b8_b9f6511ffba0["copy_current_request_context()"] 49f8280b_d7dc_110c_b848_8e7e56bfb19b["ctx.py"] f7f0b202_038e_4088_d0b8_b9f6511ffba0 -->|defined in| 49f8280b_d7dc_110c_b848_8e7e56bfb19b c1644887_6834_cf5b_9551_6a1aa07a77d7["get()"] f7f0b202_038e_4088_d0b8_b9f6511ffba0 -->|calls| c1644887_6834_cf5b_9551_6a1aa07a77d7 b9619729_0242_23b5_d6f7_5f78607ae976["copy()"] f7f0b202_038e_4088_d0b8_b9f6511ffba0 -->|calls| b9619729_0242_23b5_d6f7_5f78607ae976 a89c0022_4807_bf16_9be1_6a66f3c78c9f["ensure_sync()"] f7f0b202_038e_4088_d0b8_b9f6511ffba0 -->|calls| a89c0022_4807_bf16_9be1_6a66f3c78c9f style f7f0b202_038e_4088_d0b8_b9f6511ffba0 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/flask/ctx.py lines 153–205
def copy_current_request_context(f: F) -> F:
"""Decorate a function to run inside the current request context. This can
be used when starting a background task, otherwise it will not see the app
and request objects that were active in the parent.
.. warning::
Due to the following caveats, it is often safer (and simpler) to pass
the data you need when starting the task, rather than using this and
relying on the context objects.
In order to avoid execution switching partially though reading data, either
read the request body (access ``form``, ``json``, ``data``, etc) before
starting the task, or use a lock. This can be an issue when using threading,
but shouldn't be an issue when using greenlet/gevent or asyncio.
If the task will access ``session``, be sure to do so in the parent as well
so that the ``Vary: cookie`` header will be set. Modifying ``session`` in
the task should be avoided, as it may execute after the response cookie has
already been written.
.. code-block:: python
import gevent
from flask import copy_current_request_context
@app.route('/')
def index():
@copy_current_request_context
def do_some_work():
# do some work here, it can access flask.request or
# flask.session like you would otherwise in the view function.
...
gevent.spawn(do_some_work)
return 'Regular response'
.. versionadded:: 0.10
"""
ctx = _cv_app.get(None)
if ctx is None:
raise RuntimeError(
"'copy_current_request_context' can only be used when a"
" request context is active, such as in a view function."
)
ctx = ctx.copy()
def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
with ctx:
return ctx.app.ensure_sync(f)(*args, **kwargs)
return update_wrapper(wrapper, f) # type: ignore[return-value]
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does copy_current_request_context() do?
copy_current_request_context() is a function in the flask codebase, defined in src/flask/ctx.py.
Where is copy_current_request_context() defined?
copy_current_request_context() is defined in src/flask/ctx.py at line 153.
What does copy_current_request_context() call?
copy_current_request_context() calls 3 function(s): copy, ensure_sync, get.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free