1
1
import asyncio
2
- import contextlib
3
2
import os
4
3
import shutil
5
4
import subprocess
@@ -84,6 +83,107 @@ def uninstall_templates(install_dir):
84
83
85
84
86
85
class Templates :
86
+ class Miner :
87
+ def __init__ (self , dir , wallet , netuid ):
88
+ self .dir = dir
89
+ self .wallet = wallet
90
+ self .netuid = netuid
91
+ self .process = None
92
+
93
+ self .started = asyncio .Event ()
94
+
95
+ async def __aenter__ (self ):
96
+ self .process = await asyncio .create_subprocess_exec (
97
+ sys .executable ,
98
+ f"{ self .dir } /miner.py" ,
99
+ "--netuid" ,
100
+ str (self .netuid ),
101
+ "--subtensor.network" ,
102
+ "local" ,
103
+ "--subtensor.chain_endpoint" ,
104
+ "ws://localhost:9944" ,
105
+ "--wallet.path" ,
106
+ self .wallet .path ,
107
+ "--wallet.name" ,
108
+ self .wallet .name ,
109
+ "--wallet.hotkey" ,
110
+ "default" ,
111
+ env = {
112
+ "BT_LOGGING_INFO" : "1" ,
113
+ },
114
+ stdout = asyncio .subprocess .PIPE ,
115
+ )
116
+
117
+ self .__reader_task = asyncio .create_task (self ._reader ())
118
+
119
+ async with asyncio .timeout (30 ):
120
+ await self .started .wait ()
121
+
122
+ return self
123
+
124
+ async def __aexit__ (self , exc_type , exc_value , traceback ):
125
+ self .process .terminate ()
126
+ self .__reader_task .cancel ()
127
+
128
+ await self .process .wait ()
129
+
130
+ async def _reader (self ):
131
+ async for line in self .process .stdout :
132
+ if b"Starting main loop" in line :
133
+ self .started .set ()
134
+
135
+ class Validator :
136
+ def __init__ (self , dir , wallet , netuid ):
137
+ self .dir = dir
138
+ self .wallet = wallet
139
+ self .netuid = netuid
140
+ self .process = None
141
+
142
+ self .started = asyncio .Event ()
143
+ self .set_weights = asyncio .Event ()
144
+
145
+ async def __aenter__ (self ):
146
+ self .process = await asyncio .create_subprocess_exec (
147
+ sys .executable ,
148
+ f"{ self .dir } /validator.py" ,
149
+ "--netuid" ,
150
+ str (self .netuid ),
151
+ "--subtensor.network" ,
152
+ "local" ,
153
+ "--subtensor.chain_endpoint" ,
154
+ "ws://localhost:9944" ,
155
+ "--wallet.path" ,
156
+ self .wallet .path ,
157
+ "--wallet.name" ,
158
+ self .wallet .name ,
159
+ "--wallet.hotkey" ,
160
+ "default" ,
161
+ env = {
162
+ "BT_LOGGING_INFO" : "1" ,
163
+ },
164
+ stdout = asyncio .subprocess .PIPE ,
165
+ )
166
+
167
+ self .__reader_task = asyncio .create_task (self ._reader ())
168
+
169
+ async with asyncio .timeout (30 ):
170
+ await self .started .wait ()
171
+
172
+ return self
173
+
174
+ async def __aexit__ (self , exc_type , exc_value , traceback ):
175
+ self .process .terminate ()
176
+ self .__reader_task .cancel ()
177
+
178
+ await self .process .wait ()
179
+
180
+ async def _reader (self ):
181
+ async for line in self .process .stdout :
182
+ if b"Starting validator loop." in line :
183
+ self .started .set ()
184
+ elif b"Successfully set weights and Finalized." in line :
185
+ self .set_weights .set ()
186
+
87
187
def __init__ (self ):
88
188
self .dir = clone_or_update_templates ()
89
189
@@ -93,52 +193,8 @@ def __enter__(self):
93
193
def __exit__ (self , exc_type , exc_value , traceback ):
94
194
uninstall_templates (self .dir )
95
195
96
- @contextlib .asynccontextmanager
97
- async def miner (self , wallet , netuid ):
98
- process = await asyncio .create_subprocess_exec (
99
- sys .executable ,
100
- f"{ self .dir } /miner.py" ,
101
- "--netuid" ,
102
- str (netuid ),
103
- "--subtensor.network" ,
104
- "local" ,
105
- "--subtensor.chain_endpoint" ,
106
- "ws://localhost:9944" ,
107
- "--wallet.path" ,
108
- wallet .path ,
109
- "--wallet.name" ,
110
- wallet .name ,
111
- "--wallet.hotkey" ,
112
- "default" ,
113
- )
114
-
115
- yield
116
-
117
- process .terminate ()
118
-
119
- await process .wait ()
120
-
121
- @contextlib .asynccontextmanager
122
- async def validator (self , wallet , netuid ):
123
- process = await asyncio .create_subprocess_exec (
124
- sys .executable ,
125
- f"{ self .dir } /validator.py" ,
126
- "--netuid" ,
127
- str (netuid ),
128
- "--subtensor.network" ,
129
- "local" ,
130
- "--subtensor.chain_endpoint" ,
131
- "ws://localhost:9944" ,
132
- "--wallet.path" ,
133
- wallet .path ,
134
- "--wallet.name" ,
135
- wallet .name ,
136
- "--wallet.hotkey" ,
137
- "default" ,
138
- )
139
-
140
- yield
141
-
142
- process .terminate ()
196
+ def miner (self , wallet , netuid ):
197
+ return self .Miner (self .dir , wallet , netuid )
143
198
144
- await process .wait ()
199
+ def validator (self , wallet , netuid ):
200
+ return self .Validator (self .dir , wallet , netuid )
0 commit comments