document that headers must be set before streaming
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent f00ad42 · Browse files · 4 files·+55 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+55 added−8 removed
- other:+55 added−8 removed
Provenance
- Commit
a29f88c- Tree
e1c8601- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
f00ad42
4 files changed+55 added−8 removed
modified:docs/patterns/streaming.rst+15 added
6 unmodified lines7 roundtrip to the filesystem?8 9 The answer is by using generators and direct responses.10 11+HTTP Response Behavior12+----------------------13+14+**Headers cannot be changed after the streaming response starts.**15+16+When using streaming, it's important to be aware of the order than an HTTP17+response is sent. All headers must be sent first, then the body. More headers18+cannot be sent after the body has begun. Therefore, you must make sure all19+headers are set before starting the response, outside the generator.20+21+In particular, if the generator will access ``session``, be sure to do so in the22+view as well so that the ``Vary: cookie`` header will be set. Do not modify the23+session in the generator, as the ``Set-Cookie`` header will already be sent.24+25+26 Basic Usage27 -----------28 29 This is a basic view function that generates a lot of CSV data on the fly.71 unmodified linesmodified:docs/templating.rst+8 added-2 removed
223 unmodified lines224 def timeline():225 return stream_template("timeline.html")226 227 These functions automatically apply the228-:func:`~flask.stream_with_context` wrapper if a request is active, so229-that it remains available in the template.228+:func:`~flask.stream_with_context` wrapper if a request is active, so that229+:data:`.request`, :data:`.session`, and :data:`.g` remain available in the230+template.231+232+More headers cannot be sent after the body has begun. Therefore, you must233+make sure all headers are set before starting the response. In particular,234+if the template will access ``session``, be sure to do so in the view as235+well so that the ``Vary: cookie`` header will be set.modified:src/flask/ctx.py+20 added-6 removed
151 unmodified lines152 F = t.TypeVar("F", bound=t.Callable[..., t.Any])153 154 155 def copy_current_request_context(f: F) -> F:156- """A helper function that decorates a function to retain the current157- request context. This is useful when working with greenlets. The moment158- the function is decorated a copy of the request context is created and159- then pushed when the function is called. The current session is also160- included in the copied request context.156+ """Decorate a function to run inside the current request context. This can157+ be used when starting a background task, otherwise it will not see the app158+ and request objects that were active in the parent.159 162- Example::160+ .. warning::161 162+ Due to the following caveats, it is often safer (and simpler) to pass163+ the data you need when starting the task, rather than using this and164+ relying on the context objects.165+166+ In order to avoid execution switching partially though reading data, either167+ read the request body (access ``form``, ``json``, ``data``, etc) before168+ 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 well172+ so that the ``Vary: cookie`` header will be set. Modifying ``session`` in173+ the task should be avoided, as it may execute after the response cookie has174+ already been written.175+176+ .. code-block:: python177+178 import gevent179 from flask import copy_current_request_context180 181 @app.route('/')292 unmodified linesmodified:src/flask/helpers.py+12 added
66 unmodified lines67 request context. This keeps :data:`request`, :data:`session`, and :data:`g`68 available, even though at the point the generator runs the request context69 will typically have ended.70 71+ .. warning::72+73+ Due to the following caveat, it is often safer to pass the data you74+ need as arguments to the generator, rather than relying on the context75+ objects.76+77+ More headers cannot be sent after the body has begun. Therefore, you must78+ make sure all headers are set before starting the response. In particular,79+ if the generator will access ``session``, be sure to do so in the view as80+ well so that the ``Vary: cookie`` header will be set. Do not modify the81+ session in the generator, as the ``Set-Cookie`` header will already be sent.82+83 Use it as a decorator on a generator function:84 85 .. code-block:: python86 567 unmodified lines