Skip to content
Myr

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

4 files+55 added−8 removed

modified:docs/patterns/streaming.rst+15 added
6 unmodified lines
7roundtrip to the filesystem?89The answer is by using generators and direct responses.1011HTTP Response Behavior12----------------------1314**Headers cannot be changed after the streaming response starts.**1516When using streaming, it's important to be aware of the order than an HTTP17response is sent. All headers must be sent first, then the body. More headers18cannot be sent after the body has begun. Therefore, you must make sure all19headers are set before starting the response, outside the generator.2021In particular, if the generator will access ``session``, be sure to do so in the22view as well so that the ``Vary: cookie`` header will be set. Do not modify the23session in the generator, as the ``Set-Cookie`` header will already be sent.242526Basic Usage27-----------2829This is a basic view function that generates a lot of CSV data on the fly.
71 unmodified lines
modified:docs/templating.rst+8 added-2 removed
223 unmodified lines
224 def timeline():225 return stream_template("timeline.html")226227These functions automatically apply the228:func:`~flask.stream_with_context` wrapper if a request is active, so229that 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 the230template.231232More headers cannot be sent after the body has begun. Therefore, you must233make sure all headers are set before starting the response. In particular,234if the template will access ``session``, be sure to do so in the view as235well so that the ``Vary: cookie`` header will be set.
modified:src/flask/ctx.py+20 added-6 removed
151 unmodified lines
152F = t.TypeVar("F", bound=t.Callable[..., t.Any])153154155def 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.159162 Example::160 .. warning::161162 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.165166 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.170171 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.175176 .. code-block:: python177178 import gevent179 from flask import copy_current_request_context180181 @app.route('/')
292 unmodified lines
modified:src/flask/helpers.py+12 added
66 unmodified lines
67 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.7071 .. warning::7273 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.7677 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.8283 Use it as a decorator on a generator function:8485 .. code-block:: python86
567 unmodified lines