-
-
Notifications
You must be signed in to change notification settings - Fork 997
Description
Python Version: 3.12.8
When using asyncio.gather to concurrently execute asynchronous HTTP requests, there is a significant delay in task creation—approximately one second per task—which is unreasonable. The same code runs normally in Python 3.10, with tasks being created and executed concurrently without such delays.
`import asyncio
import time
import httpx
async def test_request(brand_id: int):
url = "https://httpbin.org/delay/1"
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.text
async def test(task_id: int):
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())} task {task_id} start")
response = await test_request(brand_id=task_id)
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())} task {task_id} end")
async def batch_test(task_ids: list[int]):
print(task_ids)
tasks = [asyncio.create_task(test(task_id)) for task_id in task_ids]
await asyncio.gather(*tasks)
if name == 'main':
task_ids = [i for i in range(10)]
asyncio.run(batch_test(task_ids))`
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2025-11-06 17:36:19 task 0 start 2025-11-06 17:36:20 task 1 start 2025-11-06 17:36:21 task 2 start 2025-11-06 17:36:22 task 3 start 2025-11-06 17:36:22 task 4 start 2025-11-06 17:36:23 task 5 start 2025-11-06 17:36:24 task 6 start 2025-11-06 17:36:24 task 7 start 2025-11-06 17:36:25 task 8 start 2025-11-06 17:36:26 task 9 start 2025-11-06 17:36:24 task 7 start 2025-11-06 17:36:25 task 8 start 2025-11-06 17:36:26 task 9 start 2025-11-06 17:36:25 task 8 start 2025-11-06 17:36:26 task 9 start 2025-11-06 17:36:28 task 5 end 2025-11-06 17:36:28 task 7 end 2025-11-06 17:36:26 task 9 start 2025-11-06 17:36:28 task 5 end 2025-11-06 17:36:28 task 7 end 2025-11-06 17:36:28 task 5 end 2025-11-06 17:36:28 task 7 end 2025-11-06 17:36:28 task 7 end 2025-11-06 17:36:28 task 0 end 2025-11-06 17:36:28 task 0 end 2025-11-06 17:36:28 task 2 end 2025-11-06 17:36:28 task 4 end 2025-11-06 17:36:28 task 4 end 2025-11-06 17:36:28 task 3 end 2025-11-06 17:36:28 task 1 end 2025-11-06 17:36:28 task 8 end 2025-11-06 17:36:28 task 6 end 2025-11-06 17:36:28 task 9 end
I don't know why this is happening