@@ -102,16 +102,33 @@ def uninstall_templates(install_dir):
102102 shutil .rmtree (install_dir )
103103
104104
105+ def get_event_loop ():
106+ """Returns the current event loop or creates a new one if there is none."""
107+ try :
108+ loop = asyncio .get_running_loop ()
109+ except RuntimeError :
110+ loop = asyncio .new_event_loop ()
111+ asyncio .set_event_loop (loop )
112+ return loop
113+
114+
105115class Templates :
106116 class Miner :
107117 def __init__ (self , dir , wallet , netuid ):
108118 self .dir = dir
109119 self .wallet = wallet
110120 self .netuid = netuid
111121 self .process = None
112-
122+ self . loop = get_event_loop ()
113123 self .started = asyncio .Event ()
114124
125+ def __enter__ (self ):
126+ self .loop .run_until_complete (self .__aenter__ ())
127+ return self
128+
129+ def __exit__ (self , exc_type , exc_value , traceback ):
130+ self .loop .run_until_complete (self .__aexit__ (exc_type , exc_value , traceback ))
131+
115132 async def __aenter__ (self ):
116133 env = os .environ .copy ()
117134 env ["BT_LOGGING_DEBUG" ] = "1"
@@ -172,6 +189,7 @@ def __init__(self, dir, wallet, netuid):
172189 self .netuid = netuid
173190 self .process = None
174191
192+ self .loop = get_event_loop ()
175193 self .started = asyncio .Event ()
176194 self .set_weights = asyncio .Event ()
177195
@@ -209,12 +227,19 @@ async def __aenter__(self):
209227
210228 return self
211229
230+ def __enter__ (self ):
231+ self .loop .run_until_complete (self .__aenter__ ())
232+ return self
233+
212234 async def __aexit__ (self , exc_type , exc_value , traceback ):
213235 self .process .terminate ()
214236 self .__reader_task .cancel ()
215237
216238 await self .process .wait ()
217239
240+ def __exit__ (self , exc_type , exc_value , traceback ):
241+ self .loop .run_until_complete (self .__aexit__ (exc_type , exc_value , traceback ))
242+
218243 async def _reader (self ):
219244 async for line in self .process .stdout :
220245 try :
0 commit comments