如何同步多个socket.io客户端访问python中的单个双端队列/集合/列表?

发布于 2025-01-11 11:56:03 字数 913 浏览 5 评论 0原文

我同时订阅多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文