websocket bug?

subscribe many coins on both spot ans swap market by below codes:


import json
import websocket


spot_ws = websocket.create_connection('wss://stream.binance.com:9443/ws')
swap_ws = websocket.create_connection('wss://fstream.binance.com/ws')
symbols = ["BTCUSDT", "ETHUSDT", "SOLUSDT", "BNBUSDT", "XRPUSDT", "DOGEUSDT", "ADAUSDT", "TRXUSDT", "AVAXUSDT",
           "LINKUSDT", "DOTUSDT", "BCHUSDT", "NEARUSDT", "LTCUSDT",  "ICPUSDT", "FETUSDT", "UNIUSDT"]
sub_list = list(map(lambda x: x.lower() + '@kline_1m', symbols))
spot_ws.send(json.dumps({"method": "SUBSCRIBE", "params": sub_list, "id": 1}))
swap_ws.send(json.dumps({"method": "SUBSCRIBE", "params": sub_list, "id": 1}))
spot_ws.recv()
swap_ws.recv()
spot_completed_num = 0
spot_completed = False
while True:
    for wsi in [spot_ws, swap_ws]:
        if spot_completed and wsi == spot_ws:
            continue
        datai = json.loads(wsi.recv())
        if datai['k']['x']:
            if wsi == spot_ws:
                spot_completed_num += 1
                print('spot', datai['k']['s'])
            elif wsi == swap_ws:
                print('swap', datai['k']['s'])
            if spot_completed_num == len(symbols):
                wsi.close()
                spot_completed = True
                print('spot closed')
        else:
            if wsi == spot_ws:
                print('spot not completed', datai['k']['s'])
            elif wsi == swap_ws:
                print('swap not completed', datai['k']['s'])


I never received completed bars from swap webscoket until the spot websocket was completed or the swap websocket was alse closed when close spot websocket.

You’d need to use an async WebSocket client to listen to multiple connections.

See websockets, for example.

Currently, in this loop

you will always first recv() from spot_ws, and only then recv() from swap_ws. It is not a “parallel recv()” or something like that.