New submission from Jinwoo PARK NANTIER <github.jin...@gmail.com>:
## Question - How can I merge the results of two or three asynchronous websocket results, defined with the library `asyncio`? - I am downloading order book data (how many people want to buy or sell something at which price) of cryptocurrencies using websockets asynchronously, but having hard time showing the results of several coins. - The desired output example is as follows, being printed at the same time : ```python XRP-BTC : The most favorable ask price is 0.00023 ETH-BTC : The most favorable ask price is 0.04 LTC-BTC : The most favorable ask price is 0.001 ``` - Each line is a result of each websocket, so what I want to do is to merge the results of several webscokets ## Code Example ```python import asyncio import websockets import ast import time import json # websocket address for the cryptocurrency exchange OKEx url = "wss://ws.okex.com:8443/ws/v5/public" # function to download orderbook data, using websocket asynchronously async def ws_orderbook5(crypto_pair): while True: try: async with websockets.connect(url) as ws: channels = [{'channel': 'books5', 'instId': f'{crypto_pair}'}] sub_param = {"op": "subscribe", "args": channels} sub_str = json.dumps(sub_param) await ws.send(sub_str) print(f"send: {sub_str}") res = await asyncio.wait_for(ws.recv(), timeout=25) while True: try: res = await asyncio.wait_for(ws.recv(), timeout=25) res = ast.literal_eval(res) print(f"{crypto-pair} : Most favorable ask price is {res['data'][0]['asks'][0][0]}") time.sleep(1) except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e: try: await ws.send('ping') print("") print("ping") res = await ws.recv() continue except Exception as e: print("Failure due to an unknown error. Stopped working") break except Exception as e: print("Failure due to an unknown error. Try working again") continue ``` - The variable `res`, which is the data downloaded from OKEx websocket looks like the following dictionary, when the argument `crypto_pair` = 'XRP-BTC' . ```python {'arg': {'channel': 'books5', 'instId': 'XRP-BTC'}, 'data': [{'asks': [['0.00002585', '4514.84', '0', '2'], ['0.00002586', '5845.946', '0', '5'], ['0.00002587', '30306.155', '0', '5'], ['0.00002588', '9974.105', '0', '7'], ['0.00002589', '3104.84', '0', '5']], 'bids': [['0.00002582', '3988', '0', '2'], ['0.00002581', '23349.817', '0', '4'], ['0.0000258', '18735.565', '0', '8'], ['0.00002579', '6429.196', '0', '6'], ['0.00002578', '3492.795', '0', '5']], 'instId': 'XRP-BTC', 'ts': '1622805157064'}]} ``` - As such what is printed on console is as follows. The argument here, for example, is "XRP-BTC" again. ``` python XRP-BTC : The most favorable ask price is 0.00023 ``` - Can anyone tell me how I can merge the result of websockets so that they can be printed at the same time? ---------- components: asyncio messages: 395082 nosy: asvetlov, hellojinwoo, yselivanov priority: normal severity: normal status: open title: How to print results of asyncio websockets at the same time? type: behavior versions: Python 3.6 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44311> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com