Skip to content

Commit 25a7a47

Browse files
author
µ
committed
processed first round of feedback, plus extra's
- ruff formatting - docstrings - variable names - type annotations - don't return ret = startup() which is annotated to return None - moved time sleep intervals to singular global, for clarity
1 parent bf95417 commit 25a7a47

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

bittensor/axon.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
from bittensor.utils import networking
6464

6565

66+
"""
67+
The quantum of time to sleep in waiting loops, in seconds.
68+
"""
69+
TIME_SLEEP_INTERVAL: float = 1e-3
70+
71+
6672
class FastAPIThreadedServer(uvicorn.Server):
6773
"""
6874
The ``FastAPIThreadedServer`` class is a specialized server implementation for the Axon server in the Bittensor network.
@@ -109,27 +115,36 @@ class FastAPIThreadedServer(uvicorn.Server):
109115
_thread: threading.Thread = None
110116
_started: bool = False
111117

112-
def set_exception(self, ex):
118+
def set_exception(self, exception: Exception) -> None:
119+
"""
120+
Set self._exception in a thread safe manner, so the worker thread can communicate exceptions to the main thread.
121+
"""
113122
with self._lock:
114-
self._exception = ex
123+
self._exception = exception
115124

116-
def get_exception(self):
125+
def get_exception(self) -> Optional[Exception]:
117126
with self._lock:
118127
return self._exception
119128

120-
def set_thread(self, thread):
129+
def set_thread(self, thread: threading.Thread):
130+
"""
131+
Set self._thread in a thread safe manner, so the main thread can get the worker thread object.
132+
"""
121133
with self._lock:
122134
self._thread = thread
123135

124-
def get_thread(self):
136+
def get_thread(self) -> Optional[threading.Thread]:
125137
with self._lock:
126138
return self._thread
127139

128-
def set_started(self, started):
140+
def set_started(self, started: bool) -> None:
141+
"""
142+
Set self._started in a thread safe manner, so the main thread can get the worker thread status.
143+
"""
129144
with self._lock:
130145
self._started = started
131146

132-
def get_started(self):
147+
def get_started(self) -> bool:
133148
with self._lock:
134149
return self._started
135150

@@ -143,9 +158,8 @@ async def startup(self, sockets: Optional[List[socket.socket]] = None) -> None:
143158
"""
144159
Adds a thread-safe call to set a 'started' flag on the object.
145160
"""
146-
ret = await super().startup(sockets)
161+
await super().startup(sockets)
147162
self.set_started(True)
148-
return ret
149163

150164
@contextlib.contextmanager
151165
def run_in_thread(self):
@@ -161,9 +175,9 @@ def run_in_thread(self):
161175
thread = threading.Thread(target=self.run, daemon=True)
162176
thread.start()
163177
try:
164-
t0 = time.time()
165-
while not self.get_started() and time.time()-t0<1:
166-
time.sleep(1e-3)
178+
time_start = time.time()
179+
while not self.get_started() and time.time() - time_start < 1:
180+
time.sleep(TIME_SLEEP_INTERVAL)
167181
if not self.get_started():
168182
raise Exception("failed to start server")
169183
yield thread
@@ -181,7 +195,7 @@ def _wrapper_run(self):
181195
while not self.should_exit:
182196
if not thread.is_alive():
183197
raise Exception("worker thread died")
184-
time.sleep(1e-3)
198+
time.sleep(TIME_SLEEP_INTERVAL)
185199
except Exception as e:
186200
self.set_exception(e)
187201

@@ -458,23 +472,25 @@ def info(self) -> "bittensor.AxonInfo":
458472
placeholder2=0,
459473
)
460474

461-
# Our instantiator should be able to test axon.exception to see if any
462-
# exception occurred.
463475
@property
464-
def exception(self):
476+
def exception(self) -> Optional[Exception]:
477+
"""
478+
Axon objects expose exceptions that occurred internally through the .exception property.
479+
"""
465480
# for future use: setting self._exception to signal an exception
466-
e = getattr(self,'_exception',None)
467-
if e:
468-
return e
481+
exception = getattr(self, "_exception", None)
482+
if exception:
483+
return exception
469484
return self.fast_server.get_exception()
470485

471-
# Our instantiator should be able to test axon.is_running() to see if all
472-
# required threads etc are running.
473-
def is_running(self):
474-
t = self.fast_server.get_thread()
475-
if t is None:
486+
def is_running(self) -> bool:
487+
"""
488+
Axon objects can be queried using .is_running() to test whether worker threads are running.
489+
"""
490+
thread = self.fast_server.get_thread()
491+
if thread is None:
476492
return False
477-
return t.is_alive()
493+
return thread.is_alive()
478494

479495
def attach(
480496
self,

0 commit comments

Comments
 (0)