MCPcopy Create free account
hub / github.com/NVIDIA/SkillSpector / try_acquire

Method try_acquire

contrib/multilingual/api_pool.py:239–258  ·  view source on GitHub ↗

Non-blocking acquire — returns a key immediately or ``None``. Unlike :meth:`acquire`, this never blocks. If a slot is available right now, return the least-loaded key; otherwise return ``None``. Useful in async contexts where blocking would stall the event loop.

(self)

Source from the content-addressed store, hash-verified

237 self._condition.wait(timeout=wait)
238
239 def try_acquire(self) -> ApiKey | None:
240 """Non-blocking acquire — returns a key immediately or ``None``.
241
242 Unlike :meth:`acquire`, this never blocks. If a slot is available
243 right now, return the least-loaded key; otherwise return ``None``.
244 Useful in async contexts where blocking would stall the event loop.
245 """
246 with self._lock:
247 self._recover_expired_keys(time.monotonic())
248 available = [k for k in self._keys if k.available]
249 if not available:
250 return None
251 key = min(available, key=lambda k: k.active_requests)
252 key.active_requests += 1
253 key.total_requests += 1
254 self._total_requests_served += 1
255 _now_active = sum(k.active_requests for k in self._keys)
256 if _now_active > self._peak_active_requests:
257 self._peak_active_requests = _now_active
258 return key
259
260 def release(self, key: ApiKey, *, success: bool = True) -> None:
261 """Release a slot on *key* back to the pool.

Calls 1

_recover_expired_keysMethod · 0.95