starlette, using a synchronous function inside a web socket

无人久伴 提交于 2021-01-07 03:33:07

问题


I'm trying to build a web socket using starlette which receives a message, runs calculations in a synchronous function and returns a response.

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        stock = await websocket.receive_text()
        stock = stock.upper()
        data = sentiment_analysis(stock=stock)
        await websocket.send_json({"score": data})

the sentiment analysis is a synchronous function.

so the text is received and the calculations are run and if I print the data variable (a dictionary), I see it's returned properly, but when I try to send it back to the client, I get the following error:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/uvicorn/protocols/websockets/websockets_impl.py", line 153, in run_asgi
    result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
  File "/usr/lib/python3/dist-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/fastapi/applications.py", line 179, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/middleware/errors.py", line 146, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/exceptions.py", line 58, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/routing.py", line 566, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/routing.py", line 283, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/dist-packages/starlette/routing.py", line 57, in app
    await func(session)
  File "/usr/local/lib/python3.8/dist-packages/fastapi/routing.py", line 228, in app
    await dependant.call(**values)
  File "./app.py", line 13, in websocket_endpoint
    stock = await websocket.receive_text()
  File "/usr/local/lib/python3.8/dist-packages/starlette/websockets.py", line 85, in receive_text
    self._raise_on_disconnect(message)
  File "/usr/local/lib/python3.8/dist-packages/starlette/websockets.py", line 80, in _raise_on_disconnect
    raise WebSocketDisconnect(message["code"])
starlette.websockets.WebSocketDisconnect: 1011  

回答1:


You need to handle WebSocketDisconnect exception when a client closes the connection. It can be something like:

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            stock = await websocket.receive_text()
            stock = stock.upper()
            data = sentiment_analysis(stock=stock)
            await websocket.send_json({"score": data})
    except WebSocketDisconnect:
        handle_exception()
        ...
    else:
        await websocket.close()


来源:https://stackoverflow.com/questions/64104899/starlette-using-a-synchronous-function-inside-a-web-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!