Boost Python API Testing Speed with Async httpx: A Practical Guide
This article explains what coroutines are, compares them with threads, outlines when to use them, introduces the async‑capable httpx library, shows installation and sample code, and demonstrates a performance test where asynchronous requests cut execution time by about 73% compared to synchronous requests.
What is a coroutine?
A coroutine is a lightweight thread-like construct that runs in user space, managed by the programmer rather than the operating system, often referred to as a "user‑space thread".
Why are coroutines better than multithreading?
The control of a coroutine is fully in the user’s hands, reducing context‑switch overhead and improving efficiency.
Coroutines use far less stack memory (≈1 KB) than OS threads (≈1 MB), allowing many more concurrent tasks.
Because coroutines run on a single thread, they avoid the need for locks; shared resources are managed by simple state checks.
Applicable & non‑applicable scenarios
Applicable: I/O‑bound workloads that involve blocking operations and require high concurrency.
Not applicable: CPU‑bound tasks that involve heavy computation, since coroutines are single‑threaded and cannot parallelize CPU work.
Exploring the async HTTP framework httpx
httpx is an open‑source library that extends the popular requests API with full async support, effectively a “async‑enabled requests”.
Installation
pip install httpxBest practice: performance comparison
We compare synchronous and asynchronous HTTP requests to a sample URL (http://www.baidu.com) performed 200 times.
Synchronous version
import asyncio
import httpx
import threading
import time
def sync_main(url, sign):
response = httpx.get(url).status_code
print(f'sync_main: {threading.current_thread()}: {sign}: {response}')
sync_start = time.time()
[sync_main(url='http://www.baidu.com', sign=i) for i in range(200)]
sync_end = time.time()
print(sync_end - sync_start)The synchronous run takes about 16.6 seconds , executing requests sequentially on the main thread.
Asynchronous version
import asyncio
import httpx
import threading
import time
client = httpx.AsyncClient()
async def async_main(url, sign):
response = await client.get(url)
status_code = response.status_code
print(f'async_main: {threading.current_thread()}: {sign}: {status_code}')
loop = asyncio.get_event_loop()
tasks = [async_main(url='http://www.baidu.com', sign=i) for i in range(200)]
async_start = time.time()
loop.run_until_complete(asyncio.wait(tasks))
async_end = time.time()
loop.close()
print(async_end - async_start)The asynchronous run finishes in roughly 4.5 seconds , a reduction of about 73% compared with the synchronous approach, while still running on a single thread.
Beyond faster HTTP calls, mastering coroutines can elevate overall testing framework design and developer skill.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
