【发布时间】:2026-01-30 18:35:01
【问题描述】:
如何以非阻塞方式处理websocket数据?
以下示例显示,当调用 ws.recv() 并且没有收到任何内容时,ws.recv() 会阻止其余代码运行:
from websocket import create_connection
url = 'ws://example.com'
ws = create_connection(url)
ws.recv() # blocks
如何实现(不阻塞):
from websocket import create_connection
url = 'ws://example.com'
ws = create_connection(url)
# non-blocking
if ws.not_empty():
ws.recv()
如何以非阻塞方式处理websocket数据?
【问题讨论】:
标签: python python-3.x python-2.7 websocket