Django + daphne + StreamingHttpResponse return chunked data not streaming data

92 Views Asked by At

I am currently developing API using StreamingHttpResponse in Django.
As far as I know, StreamingHttpResponse is supposed to output results in yield units,
but in reality, it outputs yield value's'

For example, I used generator below.

def iterator():
    for i in range(100000):
        yield {"iteration ":str(i)}

Then, the result value is expected to have one yield value each time.

{"iteration" : "1"}
{"iteration" : "2"}
...

However, in reality, it returns a certain buffer size(maybe 4000chars), waits, and then returns again.

{"iteration" : "1"}
{"iteration" : "2"}
~~
{"iteration" : "100"}

In order to actually return a value every time without a buffer like above, which settings among django, daphne, and streaminghttpresponse should I add or remove?

actual log :

2023-11-10 05:29:58,361 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,376 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,398 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,412 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,432 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,461 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,475 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,491 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,515 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,533 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
2023-11-10 05:29:58,572 DEBUG 8 --- [MainThread] daphne.http_protocol:283 HTTP response chunk for ['192.168.192.1', 0] --- [7f6f53f9c0a393a547dccbdde2c59065] [24a60a12badf08b6] [user:6549c3c2c8fd4d4d6099d6d1]
In this time, I can see response of API. 2023-11-10 05:29:58,603 DEBUG 8 --- [MainThread] daphne.http_protocol:160 HTTP b'GET' request for ['127.0.0.1', 32880] --- [-] [-] [-]
0

There are 0 best solutions below