AlexNijjar commited on
Commit
872d532
·
1 Parent(s): daa3e65

Add timeout

Browse files
Files changed (3) hide show
  1. src/app.py +2 -2
  2. src/chain_data.py +21 -13
  3. src/wandb_data.py +20 -9
src/app.py CHANGED
@@ -8,8 +8,8 @@ from wandb_data import sync
8
 
9
 
10
  def main():
11
- sync_metagraph()
12
- sync()
13
  with gr.Blocks(css=".typewriter {font-family: 'JMH Typewriter', sans-serif;}", fill_height=True, fill_width=True) as app:
14
  with gr.Tab("Leaderboard") as leaderboard_tab:
15
  dropdown = gr.Dropdown()
 
8
 
9
 
10
  def main():
11
+ sync_metagraph(timeout=1000)
12
+ sync(timeout=1000)
13
  with gr.Blocks(css=".typewriter {font-family: 'JMH Typewriter', sans-serif;}", fill_height=True, fill_width=True) as app:
14
  with gr.Tab("Leaderboard") as leaderboard_tab:
15
  dropdown = gr.Dropdown()
src/chain_data.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from datetime import datetime, timedelta
2
  from typing import TypeAlias
3
 
@@ -49,9 +50,8 @@ def fetch_vtrust():
49
  VTRUST[i] = metagraph.validator_trust[i]
50
 
51
 
52
- def fetch_updated():
53
  UPDATED.clear()
54
- block = subtensor.get_current_block()
55
  for i in range(len(metagraph.last_update)):
56
  UPDATED[i] = block - metagraph.last_update[i]
57
 
@@ -70,21 +70,22 @@ last_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
70
  last_identity_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
71
 
72
 
73
- def sync_metagraph():
74
- try:
75
- global last_sync
76
- now = datetime.now(TIMEZONE)
77
- if now - last_sync < timedelta(minutes=5):
78
- return
79
 
 
80
  print("Syncing metagraph...")
81
- last_sync = now
82
- metagraph.sync(subtensor=subtensor)
83
 
84
  fetch_weights()
85
  fetch_incentives()
86
  fetch_vtrust()
87
- fetch_updated()
88
 
89
  global last_identity_sync
90
  if now - last_identity_sync < timedelta(hours=12):
@@ -92,5 +93,12 @@ def sync_metagraph():
92
 
93
  last_identity_sync = now
94
  fetch_identities()
95
- except:
96
- ...
 
 
 
 
 
 
 
 
1
+ from concurrent.futures import ThreadPoolExecutor
2
  from datetime import datetime, timedelta
3
  from typing import TypeAlias
4
 
 
50
  VTRUST[i] = metagraph.validator_trust[i]
51
 
52
 
53
+ def fetch_updated(block: int):
54
  UPDATED.clear()
 
55
  for i in range(len(metagraph.last_update)):
56
  UPDATED[i] = block - metagraph.last_update[i]
57
 
 
70
  last_identity_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
71
 
72
 
73
+ def sync_metagraph(timeout: int = 10):
74
+ global last_sync
75
+ now = datetime.now(TIMEZONE)
76
+ if now - last_sync < timedelta(minutes=5):
77
+ return
78
+ last_sync = now
79
 
80
+ def sync_task():
81
  print("Syncing metagraph...")
82
+ block = subtensor.get_current_block()
83
+ metagraph.sync(block=block, subtensor=subtensor)
84
 
85
  fetch_weights()
86
  fetch_incentives()
87
  fetch_vtrust()
88
+ fetch_updated(block)
89
 
90
  global last_identity_sync
91
  if now - last_identity_sync < timedelta(hours=12):
 
93
 
94
  last_identity_sync = now
95
  fetch_identities()
96
+
97
+ with ThreadPoolExecutor(max_workers=1) as executor:
98
+ future = executor.submit(sync_task)
99
+ try:
100
+ future.result(timeout=timeout)
101
+ except TimeoutError:
102
+ print("Timed out while syncing metagraph")
103
+ except Exception as e:
104
+ print(f"Error occurred while syncing metagraph: {e}")
src/wandb_data.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from dataclasses import dataclass
3
  from datetime import datetime, timedelta, timezone
4
  from enum import Enum
@@ -262,19 +263,29 @@ def _fetch_current_runs(wandb_api: wandb.Api):
262
  last_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
263
 
264
 
265
- def sync():
266
  global last_sync
267
  now = datetime.now(TIMEZONE)
268
- if now - last_sync < timedelta(seconds=30):
269
  return
270
-
271
- print("Syncing runs...")
272
  last_sync = now
273
- wandb_api = wandb.Api()
274
- if not RUNS:
275
- _fetch_history(wandb_api)
276
- else:
277
- _fetch_current_runs(wandb_api)
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
 
280
  def get_current_runs() -> list[Run]:
 
1
  import os
2
+ from concurrent.futures import ThreadPoolExecutor
3
  from dataclasses import dataclass
4
  from datetime import datetime, timedelta, timezone
5
  from enum import Enum
 
263
  last_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
264
 
265
 
266
+ def sync(timeout: int = 10):
267
  global last_sync
268
  now = datetime.now(TIMEZONE)
269
+ if now - last_sync < timedelta(seconds=60):
270
  return
 
 
271
  last_sync = now
272
+
273
+ def sync_task():
274
+ print("Syncing runs...")
275
+ wandb_api = wandb.Api()
276
+ if not RUNS:
277
+ _fetch_history(wandb_api)
278
+ else:
279
+ _fetch_current_runs(wandb_api)
280
+
281
+ with ThreadPoolExecutor(max_workers=1) as executor:
282
+ future = executor.submit(sync_task)
283
+ try:
284
+ future.result(timeout=timeout)
285
+ except TimeoutError:
286
+ print("Timed out while syncing runs")
287
+ except Exception as e:
288
+ print(f"Error occurred while syncing runs: {e}")
289
 
290
 
291
  def get_current_runs() -> list[Run]: