Issue44311
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2021-06-04 13:20 by hellojinwoo, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Messages (2) | |||
---|---|---|---|
msg395082 - (view) | Author: Jinwoo PARK NANTIER (hellojinwoo) | Date: 2021-06-04 13:20 | |
## 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? |
|||
msg395085 - (view) | Author: Henk-Jaap Wagenaar (cryvate) * | Date: 2021-06-04 13:57 | |
This bug tracker is for the Python language and not for asking for help with programming. This bug should be closed. You can go to e.g. stackoverflow.com and ask the question there. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:46 | admin | set | github: 88477 |
2021-06-04 14:22:42 | hellojinwoo | set | status: open -> closed stage: resolved |
2021-06-04 13:57:23 | cryvate | set | nosy:
+ cryvate messages: + msg395085 |
2021-06-04 13:20:02 | hellojinwoo | create |