So I am trying to stream the chunks of data returned back from the sql database. The chunks seem to be streamed, however when I hit the endpoint, it shows the response at the very end when the request is completed, instead of showing the streamed data chunk by chunk. I know there are already questions about this but adding mimetype doesn't seem to work for me. I have the following code:
Any help is highly appreciated!
def generate_chunks():
result = _get_query_service(repo_url, True).stream_query(qry)
chunk_counter = 0
while True:
chunk = result.fetchmany(5)
chunk_counter += 1
if not chunk:
break
for value in chunk:
yield str(chunk)
return Response(stream_with_context(generate_chunks()), content_type='application/json', status=200)
Actually it was a small thing. The above code works.
But tools like Postman and Insomnia do not support streaming data.
If you want to see your data streamed in action, use CURL or python requests.
For CURL, you need to add --no-buffer option to see the streamed data.
curl --no-buffer -v http://localhost:8082/healthy
For Python requests, you need to add stream=True. Example:
r = requests.post('http://localhost:8082/stream_query', json=dc, stream=True)
r.encoding = 'utf-8'
for line in r.iter_content(chunk_size=10): # prints the streamed data in chunks
print(line)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With