Search code examples
pythontornado

Mixing SSEs into Tornado


I am trying to incorporate a legacy SSE Server + SSE Client with tornado. (A server that collects SSEs from processes, and distributes them to clients through a UDP socket) The first SSE GET request that we make, works perfectly. The only issue is tornado gets locked up when the user navigates away from the web app, and back. The web application will never load a second time.

I have a RequestHandler that is NOT asynchronous, and uses the client to wait in a while True loop reading from a non blocking python UDP socket. These messages are then written, and flushed to the browser. The browser successfully receives the SSEs.

In my RequestHandler the on_connection_close, and on_finish are never called. These are supposed to stop the client, and break from the while True loop. Is this because my get request is NOT a coroutine?

What is the correct way to do this in Tornado? I can show a code snippet if it's really needed, but the question should be self explanatory.


Solution

  • I was able to figure it out myself, after some experimentation.

    on_finish() was never called because I need to call finish(), and on_connection_close() was never called because it was not a coroutine. I was able to resolve my issue by using the keyword yield.

    More information can be found here: http://www.tornadoweb.org/en/stable/guide/coroutines.html