ChandimaPrabath commited on
Commit
a46919c
·
1 Parent(s): 0b7f1e4

unstable fix

Browse files
Files changed (2) hide show
  1. LoadBalancer.py +5 -5
  2. app.py +5 -2
LoadBalancer.py CHANGED
@@ -34,6 +34,8 @@ class LoadBalancer:
34
 
35
  # Initialize file structure and start prefetching
36
  self.file_structure = indexer()
 
 
37
  self.start_prefetching()
38
 
39
  # Start polling and file checking in separate threads
@@ -42,11 +44,9 @@ class LoadBalancer:
42
  polling_thread.start()
43
 
44
  def start_prefetching(self):
45
- """Start the metadata prefetching in a separate thread."""
46
- loop = asyncio.new_event_loop()
47
- asyncio.set_event_loop(loop)
48
- loop.run_in_executor(None, self.prefetch_metadata)
49
-
50
  async def prefetch_metadata(self):
51
  """Prefetch metadata for all items in the file structure."""
52
  tasks = []
 
34
 
35
  # Initialize file structure and start prefetching
36
  self.file_structure = indexer()
37
+
38
+ # Start prefetching in the FastAPI event loop
39
  self.start_prefetching()
40
 
41
  # Start polling and file checking in separate threads
 
44
  polling_thread.start()
45
 
46
  def start_prefetching(self):
47
+ """Start the metadata prefetching in the FastAPI event loop."""
48
+ asyncio.create_task(self.prefetch_metadata())
49
+
 
 
50
  async def prefetch_metadata(self):
51
  """Prefetch metadata for all items in the file structure."""
52
  tasks = []
app.py CHANGED
@@ -9,10 +9,13 @@ CACHE_DIR = os.getenv("CACHE_DIR")
9
  TOKEN = os.getenv("TOKEN")
10
  REPO = os.getenv("REPO")
11
 
12
- load_balancer = LoadBalancer(cache_dir=CACHE_DIR, token=TOKEN, repo=REPO)
13
-
14
  app = FastAPI()
15
 
 
 
 
 
 
16
  @app.get("/")
17
  def greet_json():
18
  return {"Version": "0.0.1 Alpha"}
 
9
  TOKEN = os.getenv("TOKEN")
10
  REPO = os.getenv("REPO")
11
 
 
 
12
  app = FastAPI()
13
 
14
+ @app.on_event("startup")
15
+ async def startup_event():
16
+ global load_balancer
17
+ load_balancer = LoadBalancer(cache_dir=CACHE_DIR, token=TOKEN, repo=REPO)
18
+
19
  @app.get("/")
20
  def greet_json():
21
  return {"Version": "0.0.1 Alpha"}