@@ -153,13 +153,27 @@ def add_header(response):
153153
154154
155155def copy_current_request_context (f : F ) -> F :
156- """A helper function that decorates a function to retain the current
157- request context. This is useful when working with greenlets. The moment
158- the function is decorated a copy of the request context is created and
159- then pushed when the function is called. The current session is also
160- included in the copied request context.
156+ """Decorate a function to run inside the current request context. This can
157+ be used when starting a background task, otherwise it will not see the app
158+ and request objects that were active in the parent.
161159
162- Example::
160+ .. warning::
161+
162+ Due to the following caveats, it is often safer (and simpler) to pass
163+ the data you need when starting the task, rather than using this and
164+ relying on the context objects.
165+
166+ In order to avoid execution switching partially though reading data, either
167+ read the request body (access ``form``, ``json``, ``data``, etc) before
168+ starting the task, or use a lock. This can be an issue when using threading,
169+ but shouldn't be an issue when using greenlet/gevent or asyncio.
170+
171+ If the task will access ``session``, be sure to do so in the parent as well
172+ so that the ``Vary: cookie`` header will be set. Modifying ``session`` in
173+ the task should be avoided, as it may execute after the response cookie has
174+ already been written.
175+
176+ .. code-block:: python
163177
164178 import gevent
165179 from flask import copy_current_request_context
0 commit comments