websocketについての質問です。
websocketは購読しているチャンネルからデータを連続して受信していくものだと思うのですが、
例えばある商品の価格データを受信しているとき、5分おきなどにサンプリングしてcsvやdbなどに書き込んでいきたいです。
まだあまり知識がなく、どういう設計にすればいいか定石を知らないので、どこでtime.sleepし書き込めばいいかわかりません。
どなたか詳しい方いらっしゃいましたら、ざっくりでいいのでご教授くださると有難いです。
以下が現在書いているプログラムのwebsocketの部分(一応動く)です。
python3
1import websocket 2import json 3 4class Websocket(object): 5 def __init__(self, url): 6 self.loop = True 7 self.url = url 8 websocket.enableTrace(True) 9 10 self.ws = websocket.WebSocketApp(url=・・・, 11 on_open=self.on_open, 12 on_message=self.on_message, 13 on_close=self.on_close, 14 on_error=self.on_error) 15 16 17 def run(self): 18 while self.loop: 19 self.ws.run_forever() 20 21 def on_open(self, ws): 22 channels = { 23 'op' : 'subscribe', 24 'args':[ 25 "instrument:XBTUSD" 26 ] 27 } 28 ws.send(json.dumps(channels)) 29 30 def on_message(self, ws, message): 31 message = json.loads(message) #type: dict 32 33 34 if 'data' in message: 35 if 'openValue' in message['data'][0]: 36 print(message['data'][0]['openValue']) #ここの部分を5分に1回取得する 37 38 def on_close(self, ws): 39 print("disconnect") 40 def on_error(self, ws): 41 print("error") 42if __name__ == '__main__': 43 j = Websocket() 44 j.run()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。