Skip to content

Commit

Permalink
Merge pull request #552 from roboflow/initialize-usage_collector._asy…
Browse files Browse the repository at this point in the history
…nc_lock-if-async-loop-can-be-obtained

Initialize usage_collector._async_lock only if async look can be obtained
  • Loading branch information
grzegorz-roboflow authored Jul 26, 2024
2 parents a9e391d + 2157399 commit 138b395
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions inference/usage_tracking/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

class UsageCollector:
_lock = Lock()
_async_lock = asyncio.Lock()

def __new__(cls, *args, **kwargs):
with UsageCollector._lock:
Expand All @@ -49,6 +48,13 @@ def __init__(self):
if self._queue:
return

# Async lock only for async protection, should not be shared between threads
self._async_lock = None
try:
self._async_lock = asyncio.Lock()
except Exception as exc:
logger.debug("Failed to create async lock %s", exc)

self._exec_session_id = f"{time.time_ns()}_{uuid4().hex[:4]}"

self._settings: TelemetrySettings = get_telemetry_settings()
Expand Down Expand Up @@ -452,7 +458,19 @@ async def async_record_usage(
resource_id: Optional[str] = None,
fps: float = 0,
) -> DefaultDict[str, Any]:
async with UsageCollector._async_lock:
if self._async_lock:
async with self._async_lock:
self.record_usage(
source=source,
category=category,
frames=frames,
enterprise=enterprise,
api_key=api_key,
resource_details=resource_details,
resource_id=resource_id,
fps=fps,
)
else:
self.record_usage(
source=source,
category=category,
Expand Down Expand Up @@ -549,7 +567,10 @@ def push_usage_payloads(self):
self._flush_queue()

async def async_push_usage_payloads(self):
async with UsageCollector._async_lock:
if self._async_lock:
async with self._async_lock:
self.push_usage_payloads()
else:
self.push_usage_payloads()

@staticmethod
Expand Down

0 comments on commit 138b395

Please sign in to comment.