Skip to content

Commit

Permalink
Fix ResourceLogger blocking main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Nov 21, 2023
1 parent eeb844e commit 6e9f31d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions backend/danswer/background/indexing/dask_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
import asyncio

import psutil
from dask.distributed import WorkerPlugin
Expand All @@ -18,13 +18,16 @@ def setup(self, worker: Worker) -> None:
self.worker = worker
worker.loop.add_callback(self.log_resources)

def log_resources(self) -> None:
"""Periodically log CPU and memory usage."""
async def log_resources(self) -> None:
"""Periodically log CPU and memory usage.
NOTE: must be async or else will clog up the worker indefinitely due to the fact that
Dask uses Tornado under the hood (which is async)"""
while True:
cpu_percent = psutil.cpu_percent(interval=None)
memory_available_gb = psutil.virtual_memory().available / (1024.0**3)
# You can now log these values or send them to a monitoring service
logger.debug(
f"Worker {self.worker.address}: CPU usage {cpu_percent}%, Memory available {memory_available_gb}GB"
)
time.sleep(self.log_interval)
await asyncio.sleep(self.log_interval)

1 comment on commit 6e9f31d

@vercel
Copy link

@vercel vercel bot commented on 6e9f31d Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.