vault backup: 2025-02-10 17:27:57

This commit is contained in:
2025-02-10 17:27:57 +08:00
parent 60a1c02c6a
commit 5752038f86
159 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
---
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:** Its 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, itll 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)