-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (26 loc) · 926 Bytes
/
server.py
File metadata and controls
36 lines (26 loc) · 926 Bytes
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
import asyncio
import os
from collections.abc import AsyncIterable
from blacksheep import Application, Request, get
from blacksheep.server.process import is_stopping
from blacksheep.server.sse import ServerSentEvent
app = Application(show_error_details=True)
app.serve_files("static")
# Enable the signal handler to detect when the application is stopping.
os.environ["APP_SIGNAL_HANDLER"] = "1"
@get("/events")
async def events_handler(request: Request) -> AsyncIterable[ServerSentEvent]:
i = 0
while True:
if await request.is_disconnected():
print("The request is disconnected!")
break
if is_stopping():
print("The application is stopping!")
break
i += 1
yield ServerSentEvent({"message": f"Hello World {i}"})
try:
await asyncio.sleep(1)
except asyncio.exceptions.CancelledError:
break