这是一个创建于 1070 天前的主题,其中的信息可能已经有所发展或是发生改变。
当前端切换页面时发送断开请求,后端总是要过几秒才中断连接,在这期间前端新页面的请求会堵塞住,直到后端的 websocket 连接断开才会正常请求
python:
async def status(websocket: WebSocket):
await websocket.accept()
try:
while True:
request = await websocket.receive_json()
response = {'code': '', 'message': '', 'data': ''}
await websocket.send_json(response)
time.sleep(5)
except WebSocketDisconnect:
print('关闭连接')
await websocket.close()
vue:
destroyed() {
this.websocket.close()
},
6 条回复 • 2021-11-16 18:21:24 +08:00
|
|
1
jenlors 2021-11-16 17:46:41 +08:00
为什么有一个 time.sleep(5)?
|
|
|
2
Latin 2021-11-16 17:53:06 +08:00
好家伙 flask 转 fastapi 又来一道 哈哈
|
|
|
3
Vegetable 2021-11-16 17:55:35 +08:00
time.sleep 天秀,哪怕一定要用 sleep ,请用 asyncio.sleep
|
|
|
4
vicalloy 2021-11-16 17:57:51 +08:00
别在 async 里用同步函数,sleep 换成 await asyncio.sleep(5)
|