File: frozen_client.py

package info (click to toggle)
python-sse-starlette 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 944 kB
  • sloc: python: 1,487; makefile: 134; sh: 57
file content (47 lines) | stat: -rw-r--r-- 1,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
https://github.com/sysid/sse-starlette/issues/89
Server Simulation:
Run with: uvicorn tests.integration.frozen_client:app

Client Simulation:
% curl -s -N localhost:8000/events > /dev/null
^Z (suspend process -> no consumption of messages but connection alive)

Measure resource consumption:
connections: lsof -i :8000
buffers: netstat -m
"""

import anyio
import uvicorn
from starlette.applications import Starlette
from starlette.routing import Route

from sse_starlette import EventSourceResponse


async def events(request):
    async def _event_generator():
        try:
            i = 0
            while True:
                i += 1
                if i % 100 == 0:
                    print(i)
                yield dict(data={i: " " * 4096})
                await anyio.sleep(0.001)
        finally:
            print("disconnected")

    return EventSourceResponse(_event_generator(), send_timeout=10)


app = Starlette(
    debug=True,
    routes=[
        Route("/events", events),
    ],
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="trace", log_config=None)  # type: ignore