damccorm commented on code in PR #41839: URL: https://github.com/apache/beam/pull/41839#discussion_r4755866542
########## sdks/python/apache_beam/ml/inference/vllm_inference.py: ########## @@ -68,6 +68,28 @@ _VLLM_COMPLETED_REQUESTS_METRIC = 'vllm:request_success_total' _VLLM_RUNNING_REQUESTS_METRIC = 'vllm:num_requests_running' +# Cap in-flight requests per batch to match openai's default keep-alive pool +# (100 connections). Submitting an unbounded BatchElements batch (default +# max_batch_size=10000) to asyncio.gather at once triggers quadratic scanning in +# httpcore.AsyncConnectionPool._assign_requests_to_connections(), stalling the +# asyncio event loop past openai's 5s TCP connect timeout (APITimeoutError). +_MAX_CONCURRENT_REQUESTS = 100 + + +async def _gather_bounded( + items: Sequence[Any], + request_fn: Callable[[Any], Any], + max_concurrency: Optional[int] = None) -> list[Any]: + limit = ( + _MAX_CONCURRENT_REQUESTS if max_concurrency is None else max_concurrency) + semaphore = asyncio.Semaphore(limit) + + async def _run_one(item: Any) -> Any: + async with semaphore: + return await request_fn(item) + + return await asyncio.gather(*(_run_one(item) for item in items)) Review Comment: Good catch — addressed in incremental commit `56ad40e25a2`: `_gather_bounded` now sets a `failed` guard flag before releasing the semaphore (so queued tasks already scheduled in the same event-loop tick do not invoke `request_fn`), cancels and awaits all sibling tasks before exiting `async with getAsyncVLLMClient(...)`, and adds `test_gather_bounded_cancels_waiting_tasks_on_error` (along with applying the 3 `yapf` line-wrap hunks from `PreCommit Python Formatter`). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
