teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

思っているような動作しない

2020/09/20 09:46

投稿

hazata
hazata

スコア13

title CHANGED
File without changes
body CHANGED
@@ -74,4 +74,102 @@
74
74
  ```
75
75
  を実行してもgmo_socket_board()しか接続されません。
76
76
  タイムラグはあってもいいので両方とも接続できるような方法はございますでしょうか。
77
- よろしくお願いします。
77
+ よろしくお願いします。
78
+
79
+
80
+ 追記
81
+ オリジナルのサンプルコードを見ながら下記のようにコードを書き換えてみましたが、やはり同じように初めのsocketのみが接続され、これを停止すると次のsocketが接続されるような動作をします。run_forever()で処理が一時停止しているような感じがします。thread.start_new_thread(run, ())で並列処理させているつもりですがうまく動作しません。よろしくお願いします。
82
+
83
+ ```Python
84
+ import websocket
85
+ import ast
86
+ import json
87
+ import time
88
+ try:
89
+ import thread
90
+ except ImportError:
91
+ import _thread as thread
92
+ import time
93
+
94
+
95
+ def on_message(ws, message):
96
+ dict_message = ast.literal_eval(message)
97
+ ask_price = dict_message["asks"][0]["price"] #best_ask
98
+ bid_price = dict_message["bids"][0]["price"] #best_bid
99
+ print(ask_price,bid_price)
100
+
101
+
102
+ def on_error(ws, error):
103
+ print(error)
104
+
105
+ def on_close(ws):
106
+ print("### closed ###")
107
+
108
+ def on_message2(ws2, message3):
109
+ dict_message2 = ast.literal_eval(message3)
110
+ side = dict_message2["side"]
111
+ price = dict_message2["price"]
112
+ size = dict_message2["size"]
113
+ timestamp = dict_message2["timestamp"]
114
+
115
+ print(side)
116
+ print(size)
117
+ print(price)
118
+ print(timestamp)
119
+
120
+ def on_error2(ws2, error):
121
+ print(error)
122
+
123
+ def on_close2(ws2):
124
+ print("### closed ###")
125
+
126
+
127
+ def on_open(ws):
128
+ def run(*args):
129
+
130
+ message2 = {
131
+ "command": "subscribe",
132
+ "channel": "orderbooks",
133
+ "symbol": "BTC_JPY"
134
+ }
135
+
136
+ ws.send(json.dumps(message2))
137
+
138
+
139
+ print("thread terminating...")
140
+ thread.start_new_thread(run, ())
141
+
142
+ def on_open2(ws2):
143
+ def run2(*args):
144
+
145
+ message3 = {
146
+ "command": "subscribe",
147
+ "channel": "trades",
148
+ "symbol": "BTC_JPY",
149
+ "option": "TAKER_ONLY"
150
+ }
151
+
152
+ ws2.send(json.dumps(message3))
153
+
154
+ print("thread2 terminating...")
155
+ thread.start_new_thread(run2, ())
156
+
157
+ if __name__ == "__main__":
158
+ websocket.enableTrace(True)
159
+ ws = websocket.WebSocketApp("wss://api.coin.z.com/ws/public/v1",
160
+ on_message = on_message,
161
+ on_error = on_error,
162
+ on_close = on_close)
163
+ ws2 = websocket.WebSocketApp("wss://api.coin.z.com/ws/public/v1",
164
+ on_message = on_message2,
165
+ on_error = on_error2,
166
+ on_close = on_close2)
167
+
168
+ ws.on_open = on_open
169
+ ws.run_forever()
170
+
171
+ time.sleep(5)
172
+
173
+ ws2.on_open = on_open2
174
+ ws2.run_forever()
175
+ ```

1

websocket2->websocketに修正

2020/09/20 09:46

投稿

hazata
hazata

スコア13

title CHANGED
File without changes
body CHANGED
@@ -35,8 +35,8 @@
35
35
  #web_socket Excution約定履歴
36
36
  def gmo_socket_trade():
37
37
 
38
- websocket2.enableTrace(True)
38
+ websocket.enableTrace(True)
39
- ws2 = websocket2.WebSocketApp('wss://api.coin.z.com/ws/public/v1')
39
+ ws2 = websocket.WebSocketApp('wss://api.coin.z.com/ws/public/v1')
40
40
 
41
41
  def on_open2(self):
42
42
  message2 = {