2

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.

Franson Wu
  • 31
  • 5
  • You're using ```print```. It prints on the local side, not the Client side. You need to return the values to the client as a response. – ewokx Jun 23 '23 at 04:07
  • Already returned. print is help to check whether it goes as expected. – Franson Wu Jun 23 '23 at 04:36
  • Unable to replicate, this works as expected on my machine. Maybe you need to update your packages? – Rob Jun 23 '23 at 19:55
  • Yes maybe. I am testing in a newly created python 3.9 virtualenv with packages just pip installed (with pip updated). which package could be problem? – Franson Wu Jun 24 '23 at 10:53
  • Not sure, if you just installed them they should be fine. Have you tried testing this on your local machine instead of on a remote server? – Rob Jun 24 '23 at 19:39
  • Running server code on my local machine, and test with curl -v command on the same local machine, streaming ok. Run the same code on server and test with curl -v on the same server, streaming fail. Looks like something caching it? – Franson Wu Jun 25 '23 at 00:58
  • Or the server needs special setting for sse? – Franson Wu Jun 25 '23 at 01:09
  • Oh, I tested on the server locally, not through domain and public port, but through 127.0.0.1 and local port, succeed! So, how to set the server for public service for streaming data? I think 80 port and tcp is already open, what else? – Franson Wu Jun 25 '23 at 01:13

1 Answers1

1

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)

Franson Wu
  • 31
  • 5