37 lines
1.5 KiB
Markdown
37 lines
1.5 KiB
Markdown
---
|
||
tags:
|
||
aliases:
|
||
date: 2024-11-10
|
||
time: 16:54:12
|
||
description:
|
||
---
|
||
|
||
**可以用來代替[requests](https://pypi.org/project/requests/)**
|
||
|
||
## **Why [requests](https://pypi.org/project/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, `httpx`provides 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.
|
||
|
||
```python
|
||
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.
|
||
|
||
# 參考來源
|
||
- [5 Overrated Python Libraries (And What You Should Use Instead) | by Abdur Rahman | Nov, 2024 | Python in Plain English](https://python.plainenglish.io/5-overrated-python-libraries-and-what-you-should-use-instead-106bd9ded180)
|