1.5 KiB
1.5 KiB
tags, aliases, date, time, description
| tags | aliases | date | time | description |
|---|---|---|---|---|
| 2024-11-10 | 16:54:12 |
可以用來代替requests
Why requests is Overrated:
Blocking IO: Requests is synchronous, which means each call waits for the previous call to finish. This is less than ideal when working with I/O-bound programs.
Heavy: It’s got loads of convenience baked in, but it does have a cost in terms of speed and memory footprint. Not a big deal on a simple script, but on larger systems this can be a resource hog.
What You Should Instead Use: httpx
For parallel processing of requests, httpxprovides a similar API but with asynchronous support. So, if you make many API calls, it’ll save you some time and resources because it will process those requests concurrently.
import httpx
async def fetch_data(url):
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.json()
# Simple and non-blocking
data = fetch_data("https://api.example.com/data")
Pro Tip: Asynchronous requests can reduce the processing time by a great amount if the task at hand is web scraping or ingesting data from somewhere.