| 1234567891011121314151617181920 |
- """先启动 uvicorn,再运行 python concurrency_client.py。"""
- import asyncio
- from time import perf_counter
- import httpx
- async def main():
- async with httpx.AsyncClient(base_url="http://127.0.0.1:8000", timeout=10) as client:
- started = perf_counter()
- responses = await asyncio.gather(*[client.get("/slow?delay=1") for _ in range(4)])
- for index, response in enumerate(responses, start=1):
- response.raise_for_status()
- print(f"请求 {index}: {response.json()}")
- print(f"总耗时: {perf_counter() - started:.2f} 秒")
- if __name__ == "__main__":
- asyncio.run(main())
|