Streaming response not appearing in chunks
harryjulian opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I'm using an older version of Sanic 20.3.0
as I'm working on an old project. I'm trying to stream a chunked response back to the client, which is currently just curl.
Even though I'm streaming the response back using the StreamingHttpResponse.stream()
method, the response doesn't appear in chunks in curl - it all appears simultaneously. I'm wondering how I'd get the streamed response to appear properly chunked when interacting with an external client.
Code snippet
from sanic import Blueprint, response
# Response called as so
blueprint = Blueprint(obj, filename)
async def streaming_func(response):
# chunks data, writes it to response
for chunk in chunks:
await response.write(json.dumps(chunk), "\n")
@blueprint.route("/webhook_stream", methods=["POST"])
def func(...):
# logic
return response.stream(
streaming_func(*args, **kwargs),
headers = {"Transfer-Encoding": "chunked"},
content_type="text/event-stream",
)
Expected Behavior
I expected to see the individual chunks of the request being sent back to the client one by one -- instead they all appear simultaneously.
How do you run Sanic?
As a script (app.run
or Sanic.serve
)
Operating System
Linux
Sanic Version
20.3.0
Additional context
Had to omit project specific logic as it's a commercial project.
In order to not buffer with curl you need to us -N
flag.
@ahopkins I am using the -N flag. Same behaviour calling it with requests.
import requests
with requests.post("http://localhost:5008/myroute", json = {"data": 1}, stream = True) as resp:
for line in resp.iter_lines():
if line:
print(line)
FWIW, you might find the new style streaming API easier https://sanic.dev/en/guide/advanced/streaming.html#response-streaming
If you think there is a bug, can you provide a runnable test case? The code snippet you gave apparently writes all chunks at the same time, so they would appear at the same time too (even if they were encoded as separate HTTP chunks).
Note that in server-sent events the messages are separated by an empty line, not by HTTP chunked encoding.
It is v20.3, which is unsupported. But from my memory, there was no known streaming issues back then.