My flask application implements SSE to push updates to each of my users. I.e. 
each of my users gets individual updates and therefore unique SSE connections. 
I deploy the application to mod_wsgi. Now mod_wsgi allows me to use a fixed 
number of threads (default 15) which are quickly all blocked when the number of 
users grows. This happens as the SSE connection are kept open and are therefore 
blocking the thread. Subsequent requests can't get in anymore and I get a 
Script timed out before returning headers: wsgi.pyerror.

My current workaround is to put a timer in my SSE generator and break it after 
a certain threshold. Still, with many users a fixed number of threads will 
still lead to blocked requests.

def stream():
    ...
    queue = memory.get(...) 
    @stream_with_context
    def eventStream():
        try:
            start = time.time()
            while True:
                delta = start - time.time()
                if delta > app.config["SSE_TIMEOUT"]:
                    break
               # wait for source data to be available, then push it
               try:
                   entry = queue.get( True, timeout=app.config["SSE_TIMEOUT"] - 
delta)
               except Empty:
                   break
               ev = ServerSentEvent(json.dumps(entry)) 
               yield ev.encode()
        finally:
            ...
    return Response(eventStream(), mimetype="text/event-stream")

I'd like top find a clean solution to be able to handle an arbitrary amount of 
users which are able to receive updates through SSE without blocking all 
threads in wsgi.


I posted the issue also on stackoverflow: 
https://stackoverflow.com/questions/57345375/server-sent-events-sse-blocking-threads-in-mod-wsgi

-- 
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to modwsgi+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/modwsgi/7528d624-f52b-4520-8509-0db74d31a2cd%40googlegroups.com.

Reply via email to