"SSE plugin for Starlette"
Project description
Server Sent Events for Starlette and FastAPI
Background: https://sysid.github.io/sse/
Installation:
pip install sse-starlette
Usage:
import asyncio
import uvicorn
from starlette.applications import Starlette
from starlette.routing import Route
from sse_starlette.sse import EventSourceResponse
async def numbers(minimum, maximum):
for i in range(minimum, maximum + 1):
await asyncio.sleep(0.9)
yield dict(data=i)
async def sse(request):
generator = numbers(1, 5)
return EventSourceResponse(generator)
routes = [
Route("/", endpoint=sse)
]
app = Starlette(debug=True, routes=routes)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000, log_level='info')
Output:
Caveat: SSE streaming does not work in combination with GZipMiddleware.
Be aware that for proper server shutdown your application must stop all
running tasks (generators). Otherwise you might experience the following warnings
at shutdown: Waiting for background tasks to complete. (CTRL+C to force quit).
Client disconnects need to be handled in your Request handler (see example.py):
async def endless(req: Request):
async def event_publisher():
i = 0
try:
while True:
disconnected = await req.is_disconnected()
if disconnected:
_log.info(f"Disconnecting client {req.client}")
break
i += 1
yield dict(data=i)
await asyncio.sleep(0.2)
_log.info(f"Disconnected from client {req.client}")
except asyncio.CancelledError as e:
_log.info(f"Disconnected from client (via refresh/close) {req.client}")
# Do any other cleanup, if any
raise e
return EventSourceResponse(event_publisher())
Run the tests:
make test
Changelog
0.10.0 (2021-12-14)
- base EventSourceResponse on latest starlette StreamingResponse (0.17.1) and use anyio
- breaking change:
response.wait()andresponse.stop_streaming()removed
0.9.0 (2021-10-09)
- add sse comment support
0.8.1 (2021-09-30)
- minimum required python version relaxed to python 3.6
0.8.0 (2021-08-26)
- using module-based logger instead of uvicorn logger
0.7.2 (2021-04-18)
- refactoring: Github Actions introduced into build pipeline
0.6.2 (2020-12-19)
- fix: correct shutdown signal handling in case of an endpoint which only yields sporadic messages
0.6.1 (2020-10-24)
- updated example with proper error handling
0.6.0 (2020-10-24)
- In case uvicorn is used: monkeypatch uvicorn signal-handler, in order to gracefully shutdown long-running handlers/generators.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sse-starlette-0.10.2.tar.gz.
File metadata
- Download URL: sse-starlette-0.10.2.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
429e7d1c75537e0e6f9bafb36b5b27df09145efe75fadedab03d259729029336
|
|
| MD5 |
f3d362d9df7e5142ca6b6e44930605b7
|
|
| BLAKE2b-256 |
14ef1ec77e50ae98119552f1c74361d89e2a7d74271f6d4dc54f66652cc49be4
|
File details
Details for the file sse_starlette-0.10.2-py3-none-any.whl.
File metadata
- Download URL: sse_starlette-0.10.2-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79de6dc77a48e88d48ac4759cc67cc7e13f4657c78bcec64cf67d1a88a9924dc
|
|
| MD5 |
a4f57aa039edc688e51a7aed409ef9b1
|
|
| BLAKE2b-256 |
33ea762770d1cb88f6439d2059726e9db048e39a8341996b74fa4d1e3c4ebe18
|