初心者です。
pythonで取引所のデータをリアルタイムに、非同期通信で取得する
プログラムを実行していると、最初はうまく取得してくれるのですが、そのうち
2.3分するとエラーは出ずに止まってしまいます。
取引所のリファレンスには、
[ネットワークやプログラムの問題を回避するためにping、WebSocket接続を維持するために30秒ごとにハートビートパケットを送信することをお勧めします。ws.send('{"op":"ping"}');]
とかかれていたのですがpingを送信する方法や再接続の仕方など
ご教示いただけたら幸いです。
よろしくお願いいたします。
python
import asyncio import aiohttp import time async def run_forever(url, send_json): async with session.ws_connect(url) as ws: await ws.send_json(send_json) async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: a = msg.json() print("depth {}".format(a)) elif msg.type == aiohttp.WSMsgType.ERROR: print('error') break async def main(): global session async with aiohttp.ClientSession() as session: wstask1 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BITUSDT"}})) wstask2 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BITBTC"}})) wstask3 = asyncio.create_task(run_forever("wss://stream.bybit.com/spot/quote/ws/v2", {"topic": "depth","event": "sub","params": {"symbol": "BTCUSDT"}})) await wstask1 await wstask2 await wstask3 await main()
まだ回答がついていません
会員登録して回答してみよう