File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,6 +8,21 @@ roundtrip to the filesystem?
88
99The answer is by using generators and direct responses.
1010
11+ HTTP Response Behavior
12+ ----------------------
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 HTTP
17+ response is sent. All headers must be sent first, then the body. More headers
18+ cannot be sent after the body has begun. Therefore, you must make sure all
19+ 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 the
22+ view as well so that the ``Vary: cookie `` header will be set. Do not modify the
23+ session in the generator, as the ``Set-Cookie `` header will already be sent.
24+
25+
1126Basic Usage
1227-----------
1328
Original file line number Diff line number Diff line change @@ -251,5 +251,11 @@ functions to make this easier to use.
251251 return stream_template(" timeline.html" )
252252
253253 These functions automatically apply the
254- :func: `~flask.stream_with_context ` wrapper if a request is active, so
255- that it remains available in the template.
254+ :func: `~flask.stream_with_context ` wrapper if a request is active, so that
255+ :data: `.request `, :data: `.session `, and :data: `.g ` remain available in the
256+ template.
257+
258+ More headers cannot be sent after the body has begun. Therefore, you must
259+ make sure all headers are set before starting the response. In particular,
260+ if the template will access ``session ``, be sure to do so in the view as
261+ well so that the ``Vary: cookie `` header will be set.
Original file line number Diff line number Diff line change @@ -68,6 +68,18 @@ def stream_with_context(
6868 available, even though at the point the generator runs the request context
6969 will typically have ended.
7070
71+ .. warning::
72+
73+ Due to the following caveat, it is often safer to pass the data you
74+ need as arguments to the generator, rather than relying on the context
75+ objects.
76+
77+ More headers cannot be sent after the body has begun. Therefore, you must
78+ 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 as
80+ well so that the ``Vary: cookie`` header will be set. Do not modify the
81+ session in the generator, as the ``Set-Cookie`` header will already be sent.
82+
7183 Use it as a decorator on a generator function:
7284
7385 .. code-block:: python
You can’t perform that action at this time.
0 commit comments