-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
59 lines (42 loc) · 1.67 KB
/
server.py
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
48
49
50
51
52
53
54
55
56
57
58
59
"""
FastAPI websocket implementation, for a weird producer-consumer websocket connections.
An endpoint which accepts a websocket connection gets the input stream
and forwards it to another output stream over a different websocket connection.
This script indirectly shows how you can use asyncio.Queue as a Golang-equivalent
channel to communicate in between python coroutines. That too when these corotuines
are active websockets.
"""
import asyncio
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from websockets import ConnectionClosedError
app = FastAPI()
queue = asyncio.Queue()
@app.websocket("/ws/producer")
async def producer_endpoint(websocket: WebSocket):
"""
view function which handles all the incoming payload
over websocket and acts as producer by dumping
those values into a asyncio.Queue.
"""
await websocket.accept()
try:
while True:
payload_to_be_produced = await websocket.receive_json()
await queue.put(payload_to_be_produced)
await websocket.send_json(payload_to_be_produced)
except WebSocketDisconnect:
print(f"producer dropped connection!")
@app.websocket("/ws/consumer")
async def consumer_endpoint(websocket: WebSocket):
"""
view function which sends/broadcasts the payload
from the queue over established websockets.
"""
await websocket.accept()
try:
while True:
payload_to_be_consumed = await queue.get() # blocking if empty tho.
await websocket.send_json(payload_to_be_consumed)
queue.task_done()
except (WebSocketDisconnect, ConnectionClosedError):
print(f"consumer dropped connection!")