如果我打开 Starlette/FastAPI WebSocket,如果我从协程外部关闭 WebSocket,当前等待从客户端接收数据的协程会发生什么情况?receive
是引发异常的调用还是协程永远留在内存中,因为它永远不会收到任何东西?
from fastapi import FastAPI
from fastapi.websockets import WebSocket
app = FastAPI()
open_sockets = {}
@app.websocket('/connection/')
async def connection(websocket: WebSocket):
await websocket.accept()
id = await websocket.receive_json()['id']
open_sockets[id] = websocket
while True:
data = await websocket.receive_json()
@app.post('/kill/{id}/')
async def kill(id=str):
# What does this do to the above `await websocket.receive_json()`?
await open_sockets[id].close()
del open_sockets[id]