Horje

Python - Python Server Sent Events
Start
  Flask SSE
@route("/stream")
def stream():
    def event_stream():
        while True:
            if message_to_send:
                yield "data: 
                    {}\n\n".format(message_to_send)"
    
    return Response(event_stream(), mimetype="text/event-stream")

  Asyncio SSE

This example uses the asyncio SSE library: https://github.com/brutasse/asyncio-sse (opens new window)

import asyncio
import sse

class Handler(sse.Handler):
    @asyncio.coroutine
    def handle_request(self):
        yield from asyncio.sleep(2)
        self.send('foo')
        yield from asyncio.sleep(2)
        self.send('bar', event='wakeup')

start_server = sse.serve(Handler, 'localhost', 8888)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


Read Article
https://develop.horje.com/learn/1434/reference