質問編集履歴

2

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

2020/09/20 09:46

投稿

hazata
hazata

スコア13

test CHANGED
File without changes
test CHANGED
@@ -151,3 +151,199 @@
151
151
  タイムラグはあってもいいので両方とも接続できるような方法はございますでしょうか。
152
152
 
153
153
  よろしくお願いします。
154
+
155
+
156
+
157
+
158
+
159
+ 追記
160
+
161
+ オリジナルのサンプルコードを見ながら下記のようにコードを書き換えてみましたが、やはり同じように初めのsocketのみが接続され、これを停止すると次のsocketが接続されるような動作をします。run_forever()で処理が一時停止しているような感じがします。thread.start_new_thread(run, ())で並列処理させているつもりですがうまく動作しません。よろしくお願いします。
162
+
163
+
164
+
165
+ ```Python
166
+
167
+ import websocket
168
+
169
+ import ast
170
+
171
+ import json
172
+
173
+ import time
174
+
175
+ try:
176
+
177
+ import thread
178
+
179
+ except ImportError:
180
+
181
+ import _thread as thread
182
+
183
+ import time
184
+
185
+
186
+
187
+
188
+
189
+ def on_message(ws, message):
190
+
191
+ dict_message = ast.literal_eval(message)
192
+
193
+ ask_price = dict_message["asks"][0]["price"] #best_ask
194
+
195
+ bid_price = dict_message["bids"][0]["price"] #best_bid
196
+
197
+ print(ask_price,bid_price)
198
+
199
+
200
+
201
+
202
+
203
+ def on_error(ws, error):
204
+
205
+ print(error)
206
+
207
+
208
+
209
+ def on_close(ws):
210
+
211
+ print("### closed ###")
212
+
213
+
214
+
215
+ def on_message2(ws2, message3):
216
+
217
+ dict_message2 = ast.literal_eval(message3)
218
+
219
+ side = dict_message2["side"]
220
+
221
+ price = dict_message2["price"]
222
+
223
+ size = dict_message2["size"]
224
+
225
+ timestamp = dict_message2["timestamp"]
226
+
227
+
228
+
229
+ print(side)
230
+
231
+ print(size)
232
+
233
+ print(price)
234
+
235
+ print(timestamp)
236
+
237
+
238
+
239
+ def on_error2(ws2, error):
240
+
241
+ print(error)
242
+
243
+
244
+
245
+ def on_close2(ws2):
246
+
247
+ print("### closed ###")
248
+
249
+
250
+
251
+
252
+
253
+ def on_open(ws):
254
+
255
+ def run(*args):
256
+
257
+
258
+
259
+ message2 = {
260
+
261
+ "command": "subscribe",
262
+
263
+ "channel": "orderbooks",
264
+
265
+ "symbol": "BTC_JPY"
266
+
267
+ }
268
+
269
+
270
+
271
+ ws.send(json.dumps(message2))
272
+
273
+
274
+
275
+
276
+
277
+ print("thread terminating...")
278
+
279
+ thread.start_new_thread(run, ())
280
+
281
+
282
+
283
+ def on_open2(ws2):
284
+
285
+ def run2(*args):
286
+
287
+
288
+
289
+ message3 = {
290
+
291
+ "command": "subscribe",
292
+
293
+ "channel": "trades",
294
+
295
+ "symbol": "BTC_JPY",
296
+
297
+ "option": "TAKER_ONLY"
298
+
299
+ }
300
+
301
+
302
+
303
+ ws2.send(json.dumps(message3))
304
+
305
+
306
+
307
+ print("thread2 terminating...")
308
+
309
+ thread.start_new_thread(run2, ())
310
+
311
+
312
+
313
+ if __name__ == "__main__":
314
+
315
+ websocket.enableTrace(True)
316
+
317
+ ws = websocket.WebSocketApp("wss://api.coin.z.com/ws/public/v1",
318
+
319
+ on_message = on_message,
320
+
321
+ on_error = on_error,
322
+
323
+ on_close = on_close)
324
+
325
+ ws2 = websocket.WebSocketApp("wss://api.coin.z.com/ws/public/v1",
326
+
327
+ on_message = on_message2,
328
+
329
+ on_error = on_error2,
330
+
331
+ on_close = on_close2)
332
+
333
+
334
+
335
+ ws.on_open = on_open
336
+
337
+ ws.run_forever()
338
+
339
+
340
+
341
+ time.sleep(5)
342
+
343
+
344
+
345
+ ws2.on_open = on_open2
346
+
347
+ ws2.run_forever()
348
+
349
+ ```

1

websocket2->websocketに修正

2020/09/20 09:46

投稿

hazata
hazata

スコア13

test CHANGED
File without changes
test CHANGED
@@ -72,9 +72,9 @@
72
72
 
73
73
 
74
74
 
75
- websocket2.enableTrace(True)
75
+ websocket.enableTrace(True)
76
76
 
77
- ws2 = websocket2.WebSocketApp('wss://api.coin.z.com/ws/public/v1')
77
+ ws2 = websocket.WebSocketApp('wss://api.coin.z.com/ws/public/v1')
78
78
 
79
79
 
80
80