初心者です。
pythonで取引所のデータをリアルタイムに、非同期通信で取得する
プログラムを実行していると、最初はうまく取得してくれるのですが、そのうち
2.3分するとエラーは出ずに止まってしまいます。
取引所のリファレンスには、
[ネットワークやプログラムの問題を回避するためにping、WebSocket接続を維持するために30秒ごとにハートビートパケットを送信することをお勧めします。ws.send('{"op":"ping"}');]
とかかれていたのですがpingを送信する方法や再接続の仕方など
ご教示いただけたら幸いです。
よろしくお願いいたします。
python
1import asyncio 2import aiohttp 3import time 4 5async def run_forever(url, send_json): 6 7 async with session.ws_connect(url) as ws: 8 9 await ws.send_json(send_json) 10 async for msg in ws: 11 if msg.type == aiohttp.WSMsgType.TEXT: 12 13 a = msg.json() 14 15 print("depth {}".format(a)) 16 17 elif msg.type == aiohttp.WSMsgType.ERROR: 18 19 print('error') 20 break 21 22async def main(): 23 global session 24 async with aiohttp.ClientSession() as session: 25 wstask1 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BITUSDT"}})) 26 wstask2 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BITBTC"}})) 27 wstask3 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BTCUSDT"}})) 28 29 await wstask1 30 await wstask2 31 await wstask3 32 33 34await main()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/01 05:36