(
self,
requests: Sequence[str | Request],
*,
forefront: bool = False,
batch_size: int = 1000,
wait_time_between_batches: timedelta = timedelta(seconds=1),
wait_for_all_requests_to_be_added: bool = False,
wait_for_all_requests_to_be_added_timeout: timedelta | None = None,
)
| 199 | |
| 200 | @override |
| 201 | async def add_requests( |
| 202 | self, |
| 203 | requests: Sequence[str | Request], |
| 204 | *, |
| 205 | forefront: bool = False, |
| 206 | batch_size: int = 1000, |
| 207 | wait_time_between_batches: timedelta = timedelta(seconds=1), |
| 208 | wait_for_all_requests_to_be_added: bool = False, |
| 209 | wait_for_all_requests_to_be_added_timeout: timedelta | None = None, |
| 210 | ) -> None: |
| 211 | transformed_requests = self._transform_requests(requests) |
| 212 | wait_time_secs = wait_time_between_batches.total_seconds() |
| 213 | |
| 214 | # Wait for the first batch to be added |
| 215 | first_batch = transformed_requests[:batch_size] |
| 216 | if first_batch: |
| 217 | await self._process_batch( |
| 218 | first_batch, |
| 219 | base_retry_wait=wait_time_between_batches, |
| 220 | forefront=forefront, |
| 221 | ) |
| 222 | |
| 223 | async def _process_remaining_batches() -> None: |
| 224 | for i in range(batch_size, len(transformed_requests), batch_size): |
| 225 | batch = transformed_requests[i : i + batch_size] |
| 226 | await self._process_batch( |
| 227 | batch, |
| 228 | base_retry_wait=wait_time_between_batches, |
| 229 | forefront=forefront, |
| 230 | ) |
| 231 | if i + batch_size < len(transformed_requests): |
| 232 | await asyncio.sleep(wait_time_secs) |
| 233 | |
| 234 | # Create and start the task to process remaining batches in the background |
| 235 | remaining_batches_task = asyncio.create_task( |
| 236 | _process_remaining_batches(), |
| 237 | name='request_queue_process_remaining_batches_task', |
| 238 | ) |
| 239 | |
| 240 | self._add_requests_tasks.append(remaining_batches_task) |
| 241 | remaining_batches_task.add_done_callback(lambda _: self._add_requests_tasks.remove(remaining_batches_task)) |
| 242 | |
| 243 | # Wait for all tasks to finish if requested |
| 244 | if wait_for_all_requests_to_be_added: |
| 245 | await wait_for_all_tasks_for_finish( |
| 246 | (remaining_batches_task,), |
| 247 | logger=logger, |
| 248 | timeout=wait_for_all_requests_to_be_added_timeout, |
| 249 | ) |
| 250 | |
| 251 | async def fetch_next_request(self) -> Request | None: |
| 252 | """Return the next request in the queue to be processed. |
nothing calls this directly
no test coverage detected