Server Side Event未接收:FastAPI部署ARM设备后异常排查
SSE实时流功能部署ARM设备后Device B数据异常排查问题
背景与运行逻辑
- 已实现基于Server Side Event(SSE)的浏览器实时流功能,后端采用Python/FastAPI,开发阶段在工作站运行完全符合预期
- 网络中自定义设备发送心跳/健康信息,后端接收处理后通过ChartJS展示到浏览器;当前共3台设备:1台Device A,2台同类型Device B
- 浏览器离开实时图表页面时,后端会短时间存储数据,返回页面时自动填充最近60秒的历史数据
- 采用异步队列实现:Device A和两台Device B各有独立队列,队列中的数据包为JSON格式,内含设备ID
部署后异常现象
- Device A的流数据与历史数据刷新均正常
- 访问Device B时,仅能接收到两台设备中一台的实时数据,刷新页面可能切换接收的设备;离开再返回页面时,两台设备的历史数据均能填充,但仅一台能实时更新
初步排查结果
- 单设备数据量约100字节,历史数据批量刷新(单次最多120个200字节数据包)正常,排除性能问题
- JS控制台日志显示仅收到一台Device B的数据
- Python端
print()语句显示消息已发送 - Wireshark抓包显示两台Device B的数据包均已到达PC,payload正确
相关代码片段
前端JS(截断版)
source.onmessage = function (event) { /* This line only shows 1 of the 2 packets being received */ console.log(event.data); /* comes over with single quotes, which is not json friendly */ var data = event.data.replace(/'/g, '"'); const json_data = JSON.parse(data);
后端Python SSE代码
@app.get("/data") async def data(): async def get_data(): # clear the queue first, then fill it up await interface.empty_queue() # this loads the queue with historic data - up to 120 packets of 200 bytes each for item in interface.objs.keys(): await interface.objs[item].load_queue(interface.health_queue, False) while True: # process the queue message = await interface.health_queue.get() print(message) yield f"data:{message}\n\n" interface.health_queue.task_done()
请求协助
抓包显示两台Device B的数据包均正常到达,但浏览器仅处理其中一台的数据,需定位问题根源。
内容的提问来源于stack exchange,提问作者jgauthier
相关产品推荐
相关产品推荐

