Get batch of experience.
(self, batch_size: int, timeout: float, min_model_version: int = 0)
| 376 | await self.writer.write(exp_list) |
| 377 | |
| 378 | async def get_batch(self, batch_size: int, timeout: float, min_model_version: int = 0) -> bytes: |
| 379 | """Get batch of experience.""" |
| 380 | await self.queue.set_min_model_version(min_model_version) |
| 381 | start_time = time.time() |
| 382 | result = [] |
| 383 | while len(result) < batch_size: |
| 384 | while len(self.exp_pool) > 0 and len(result) < batch_size: |
| 385 | exp = self.exp_pool.popleft() |
| 386 | if min_model_version > 0 and exp.info["model_version"] < min_model_version: |
| 387 | continue |
| 388 | result.append(exp) |
| 389 | if len(result) >= batch_size: |
| 390 | break |
| 391 | |
| 392 | if self.queue.stopped(): |
| 393 | # If the queue is stopped, ignore the rest of the experiences in the pool |
| 394 | raise StopAsyncIteration("Queue is closed and no more items to get.") |
| 395 | try: |
| 396 | exp_list = await asyncio.wait_for(self.queue.get(), timeout=1.0) |
| 397 | self.exp_pool.extend(exp_list) |
| 398 | except asyncio.TimeoutError: |
| 399 | if time.time() - start_time > timeout: |
| 400 | self.logger.error( |
| 401 | f"Timeout when waiting for experience, only get {len(self.exp_pool)} experiences.\n" |
| 402 | "This phenomenon is usually caused by the workflow not returning enough " |
| 403 | "experiences or running timeout. Please check your workflow implementation." |
| 404 | ) |
| 405 | batch = list(self.exp_pool) |
| 406 | self.exp_pool.clear() |
| 407 | return Experience.serialize_many(batch) |
| 408 | return Experience.serialize_many(result) |
| 409 | |
| 410 | @classmethod |
| 411 | def get_wrapper(cls, config: StorageConfig): |
nothing calls this directly
no test coverage detected