1 year ago

#249673

test-img

Userqhl

How can I synchronize multiple socket.io clients accessing a single deque/set/list in python?

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)

python

python-3.x

synchronization

python-socketio

0 Answers

Your Answer

Accepted video resources