MCPcopy Create free account
hub / github.com/Mercury7353/PyBench / RandomPool

Class RandomPool

llms/api_key_pool.py:240–266  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

238
239
240class RandomPool(Pool):
241 def __init__(self, client_args: ClientArgs) -> None:
242 super().__init__(client_args)
243 logger.info(f"initializing RandomPool with {len(client_args)} api keys")
244 logger.info(f"client args: {client_args}")
245
246 def __enter__(self, *args, **kwargs) -> ClientArgs:
247 """Get a client from the pool and return it as a context manager."""
248
249 if len(self) == 0:
250 raise APIKeyPoolEmptyError("api key pool is empty")
251
252 self._index = random.randint(0, len(self) - 1)
253 return self.queue[self._index].client_args
254
255 def __exit__(self, exc_type, exc_value, exc_traceback) -> bool:
256 """Put the resource back in the pool."""
257
258 if exc_value is not None:
259 if (
260 exc_type is RateLimitError
261 and exc_value.status_code == 429
262 and judge_quota_exceeded(exc_value.message)
263 ):
264 self._remove_cur_arg()
265 return False
266 return True
267
268
269# class SpinPool(metaclass=Singleton):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected