Server side:
from flask import Flask, request, Response, stream_with_context
import time
app = Flask(__name__)
# Define a route /stream that handles POST requests
@app.route('/stream', methods=['POST']) # GET also been tried, no difference
def stream():
@stream_with_context
def generate():
print('1')
yield "Hello\n"
time.sleep(1) # Simulate some delay
print('2')
yield "World\n"
time.sleep(1)
print('3')
yield "This is\n"
time.sleep(1)
print('4')
yield "Streaming data\n"
time.sleep(1)
print('5')
return Response(generate(), content_type='text/event-stream')
if __name__ == '__main__':
app.run(debug=True)
Client side:
import requests
import sseclient
reqUrl = 'https://api.my-domain-name.com/stream'
headers={'Accept': 'text/event-stream'}
# response = requests.get(url=reqUrl, headers=headers, stream=True)
response = requests.post(url=reqUrl, headers=headers, stream=True)
if response.status_code == 200:
for chunk in response.iter_content(chunk_size=3):
if chunk:
print(chunk)
else:
print('fail:', response.status_code)
# sse tried, no difference
# client = sseclient.SSEClient(response)
# for event in client.events():
# print(event.data)
When client connected, there was 1 2 3 4 5 printed one by one at the server side.
But client printed nothing, until server finished, client printed all data.
Help!
Have tried the code above, and GET / POST tried, with or without sse. When I tried GET method, I also tried curl -v command, the same result, data comes together after about 5 seconds, not one by one. I expect stream data, that is, each yield data can be handled in time separately, not together.
Turned out to be the server setting problem, this post 10 years ago does help. nginx setting is important.
Now server side is ok, tested by postman on my local machine, the strange is, the client side code does not go streaming, it prints all data at the same time. Have tried copy postman test header but not help.
headers={
'Accept': 'text/event-stream',
# "Content-Type": "application/json", # in postman is this, tested no difference
"Content-Type": "text/event-stream",
"Transfer-Encoding": "chunked" #tried, no help
# below are copied from postman
"Cache-Control":"no-cache",
"Connection":"keep-alive",
'Accept':'*/*',
'User-Agent':'PostmanRuntime/7.32.3'
}
BTW: client code can call some third party streaming data api correctly. So I am confusing, where is the problem.
Update: Finally solved the problem. sseclient and sseclient-py are different, I uninstall them and install only one, then ok. for me, I use sseclient, and modified some code, to make GET and POST all work well. sseclient should also work but I have not tried yet. (simply trying still have problem, need investigation)
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