Concurrency issues with server-sent events in Python

15 Views Asked by At

I am trying to implement server-sent events in my Python flask application in order to push notifications / alerts to clients from the server.

Here is the relevant code:

from random import random

message_list = []
message_lock = Lock()

def event_stream():
    while True:
        with message_lock:
            if message_list:
                message = message_list.pop(0)
                yield f"data: {message}\n\n"
        time.sleep(1)


@app.route('/stream')
def stream():
    return Response(event_stream(), mimetype="text/event-stream")

@app.route("/new_message")
def new_message():
    message_list.append(random())
    return Response(status=204)

As a simple test, I have set up a button, which when clicked calls the new_message route and pushes a new "update" to a list (just a random number for now). What I would then like is for all connected clients to receive this message.

I have set up this javascript code for clients to listen to the stream:

var source = new EventSource("/stream");
source.onmessage = function(event) {
     console.log(event.data);
};

which seems to be working since I am getting console logs as expected. HOWEVER, the issue seems to be that if multiple clients are connected, then multiple clients are calling messages_list.pop(0), which results in clients only getting some of the notifications rather than all of them.

How might I go about fixing this issue?

Thanks in advance!

I tried to introduce some thread locking so that only one thread can enter the function event_stream() at a time, but it did not fix my issue.

0

There are 0 best solutions below