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.

Author hellojinwoo
Recipients asvetlov, hellojinwoo, yselivanov
Date 2021-06-04.13:20:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1622812802.67.0.29847846911.issue44311@roundup.psfhosted.org>
In-reply-to
Content
## 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?
History
Date User Action Args
2021-06-04 13:20:02hellojinwoosetrecipients: + hellojinwoo, asvetlov, yselivanov
2021-06-04 13:20:02hellojinwoosetmessageid: <1622812802.67.0.29847846911.issue44311@roundup.psfhosted.org>
2021-06-04 13:20:02hellojinwoolinkissue44311 messages
2021-06-04 13:20:01hellojinwoocreate