如何同步多个socket.io客户端访问python中的单个双端队列/集合/列表?
我同时订阅多个 socket.io 服务器,检查所有传入条目是否已被另一个 websocket 客户端处理/接收。服务器向我提供应添加到单个列表中的数据,列表的顺序很重要,因为它代表来自交易所的订单簿。一个条目不应添加两次。 使用我当前的解决方案,我遇到了竞争条件,据我所知,socket.io 正在使用 asyncio,简单的 threading.Lock 将无济于事。
sio = socketio.Client()
sio1 = socketio.Client()
locker = threading.Lock()
added_Orders = collections.deque(maxlen=40)
@sio.on('connect')
def connect():
print('connection established')
@sio1.on('connect')
def connect():
print('connection1 established')
@sio.on('add_order', namespace="/market")
def add_order(data):
with locker:
if data in added_Orders:
return
else:
added_Orders.append(data)
webRecv.process_add(data)
@sio1.on('add_order', namespace="/market")
def add_order1(data):
with locker:
if data in added_Orders:
return
else:
added_Orders.append(data)
webRecv.process_add(data)
I am subscribing to multiple socket.io servers at the same time, all incoming entries get checked whether they have been already processed/received by another websocket client. The servers provide me with data that should be added into a single list, the order of the list is important, as it represents an orderbook from an exchange. An entry should not be added twice.
With my current solution I am experiencing race conditions, as far as I know socket.io is using asyncio, a simple threading.Lock won't help.
sio = socketio.Client()
sio1 = socketio.Client()
locker = threading.Lock()
added_Orders = collections.deque(maxlen=40)
@sio.on('connect')
def connect():
print('connection established')
@sio1.on('connect')
def connect():
print('connection1 established')
@sio.on('add_order', namespace="/market")
def add_order(data):
with locker:
if data in added_Orders:
return
else:
added_Orders.append(data)
webRecv.process_add(data)
@sio1.on('add_order', namespace="/market")
def add_order1(data):
with locker:
if data in added_Orders:
return
else:
added_Orders.append(data)
webRecv.process_add(data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论