回答編集履歴

6

 

2023/02/07 11:30

投稿

退会済みユーザー
test CHANGED
@@ -19,7 +19,6 @@
19
19
 
20
20
 
21
21
  ### 追記
22
- + eventloop をいじるのは古い書き方です。いつか廃止されて動かなくなります。
23
22
 
24
23
  + 非同期使うときはちゃんと、Gracefully にストップできるように考慮しましょう。大切なリソースなので、close_connection を使いましょう。質問文のままで 止めるのにCtrl+C乱発してたらそのうちソケットが枯渇するかもしれませんので。
25
24
  ```py

5

追記

2023/02/07 11:19

投稿

退会済みユーザー
test CHANGED
@@ -16,3 +16,55 @@
16
16
  task1 = asyncio.create_task(trades(symbol))
17
17
  ```
18
18
  でいいでしょう。
19
+
20
+
21
+ ### 追記
22
+ + eventloop をいじるのは古い書き方です。いつか廃止されて動かなくなります。
23
+
24
+ + 非同期使うときはちゃんと、Gracefully にストップできるように考慮しましょう。大切なリソースなので、close_connection を使いましょう。質問文のままで 止めるのにCtrl+C乱発してたらそのうちソケットが枯渇するかもしれませんので。
25
+ ```py
26
+ from __future__ import annotations
27
+ import asyncio
28
+ from binance import AsyncClient, DepthCacheManager, BinanceSocketManager, ThreadedDepthCacheManager
29
+
30
+ clients = []
31
+
32
+ async def main(symbol):
33
+ task1 = asyncio.create_task(trades(symbol))
34
+ task2 = asyncio.create_task(depth(symbol))
35
+ await asyncio.gather(task1, task2)
36
+
37
+ async def trades(symbol):
38
+ client = await AsyncClient.create()
39
+ clients.append(client)
40
+ bsm = BinanceSocketManager(client)
41
+
42
+ async with bsm.individual_symbol_ticker_futures_socket(symbol) as fs:
43
+ while True:
44
+ res = await fs.recv()
45
+ print(res['data']['c'])
46
+
47
+ async def depth(symbol):
48
+ client = await AsyncClient.create()
49
+ clients.append(client)
50
+ dcm = DepthCacheManager(client, symbol)
51
+ async with dcm as dcm_socket:
52
+ while True:
53
+ depth_cache = await dcm_socket.recv()
54
+ print("symbol {}".format(depth_cache.symbol))
55
+ print("top 5 bids")
56
+ print(depth_cache.get_bids()[:5])
57
+ print("top 5 asks")
58
+ print(depth_cache.get_asks()[:5])
59
+ print("last update time {}".format(depth_cache.update_time))
60
+
61
+ async def astop():
62
+ [await client.close_connection() for client in clients]
63
+ print("stop.")
64
+
65
+ if __name__ == "__main__":
66
+ try:
67
+ asyncio.run(main('ETHUSDT'))
68
+ except KeyboardInterrupt:
69
+ asyncio.run(astop())
70
+ ```

4

 

2023/02/07 10:44

投稿

退会済みユーザー
test CHANGED
@@ -12,6 +12,7 @@
12
12
  ```
13
13
  ここが並行動作にならない原因です。ここはシンプルに
14
14
  ```py
15
+ # t = await trades(symbol) #削除
15
16
  task1 = asyncio.create_task(trades(symbol))
16
17
  ```
17
18
  でいいでしょう。

3

 

2023/02/07 10:42

投稿

退会済みユーザー
test CHANGED
@@ -1,5 +1,8 @@
1
1
  1.depth コルーチンの中で thread ベースのクライアント使ってるようですが、
2
+ 多分非同期Client がなかった時代の古いバージョンの使い方と混在してると思います。
2
- 多分非同期がなかった時代の古いバージョンの使い方と混在してるので、 AsyncClient と DepthCacheManagerを使用して下さい。
3
+ AsyncClient と DepthCacheManagerを使用して下さい。
4
+
5
+ [DepthCacheManager or OptionsDepthCacheManager Usage](https://python-binance.readthedocs.io/en/stable/depth_cache.html#depthcachemanager-or-optionsdepthcachemanager-usage)
3
6
 
4
7
  2.そのうえで
5
8
  ```py

2

 

2023/02/07 10:41

投稿

退会済みユーザー
test CHANGED
@@ -1,5 +1,5 @@
1
1
  1.depth コルーチンの中で thread ベースのクライアント使ってるようですが、
2
- 多分非同期がなかった時代の古いバージョンの使い方と混在してるので、 AsyncClient DepthManagerを使用して下さい。
2
+ 多分非同期がなかった時代の古いバージョンの使い方と混在してるので、 AsyncClient DepthCacheManagerを使用して下さい。
3
3
 
4
4
  2.そのうえで
5
5
  ```py

1

 

2023/02/07 10:40

投稿

退会済みユーザー
test CHANGED
@@ -1,5 +1,5 @@
1
- 1.depth コルーチンの中で thread ベースのクライアント使ってるのは意味不明です
1
+ 1.depth コルーチンの中で thread ベースのクライアント使ってるようですが、
2
- 多分非同期がなかった時代の古いバージョンの使い方と混在してるので、ちゃんと AsyncClient の DepthManagerを使用して下さい。
2
+ 多分非同期がなかった時代の古いバージョンの使い方と混在してるので、 AsyncClient の DepthManagerを使用して下さい。
3
3
 
4
4
  2.そのうえで
5
5
  ```py
@@ -7,7 +7,7 @@
7
7
  t = await trades(symbol)
8
8
  task1 = asyncio.create_task(t)
9
9
  ```
10
- ここもやってること意味不明です。シンプルに
10
+ ここが並行動作にならない原因です。ここはシンプルに
11
11
  ```py
12
12
  task1 = asyncio.create_task(trades(symbol))
13
13
  ```