Skip to content

Instantly share code, notes, and snippets.

Created Aug 13, 2021
What would you like to do?
import asyncio
import uvicorn
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.routing import Route
from sse_starlette.sse import EventSourceResponse, unpatch_uvicorn_signal_handler
# unpatch_uvicorn_signal_handler() # if you want to rollback monkeypatching of uvcorn signal-handler
from uvicorn.config import logger as _log
html_sse = """
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC">
var evtSource = new EventSource("/numbers");
console.log("evtSource: ", evtSource);
evtSource.onmessage = function(e) {
document.getElementById('response').innerText =;
if ( == 20) {
console.log("Closing connection after 20 numbers.")
<h1>Response from server:</h1>
<div id="response"></div>
async def numbers(minimum, maximum):
""" Simulates and limited stream """
for i in range(minimum, maximum + 1):
await asyncio.sleep(0.9)
yield dict(data=i)
async def endless(req: Request):
"""Simulates and endless stream
In case of server shutdown the running task has to be stopped via signal handler in order
to enable proper server shutdown. Otherwise there will be dangling tasks preventing proper shutdown.
async def event_publisher():
i = 0
while True:
disconnected = await req.is_disconnected()
if disconnected:"Disconnecting client {req.client}")
# yield dict(id=..., event=..., data=...)
i += 1
yield dict(data=i)
await asyncio.sleep(0.9)"Disconnected from client {req.client}")
except asyncio.CancelledError as e:"Disconnected from client (via refresh/close) {req.client}")
# Do any other cleanup, if any
raise e
return EventSourceResponse(event_publisher())
async def sse(request):
generator = numbers(1, 25)
return EventSourceResponse(generator)
async def home(req: Request):
return HTMLResponse(html_sse)
routes = [
Route("/", endpoint=home),
Route("/numbers", endpoint=sse),
Route("/endless", endpoint=endless),
app = Starlette(debug=True, routes=routes)
if __name__ == "__main__":, host="", port=8000, log_level="trace")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment