惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Recent Announcements
Recent Announcements
Jina AI
Jina AI
G
Google Developers Blog
腾讯CDC
博客园_首页
博客园 - 【当耐特】

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I Rewrote Our Scraper with asyncio. My CTO Thought I Adde...
BAOFUFAN · 2026-05-01 · via DEV Community

BAOFUFAN

Here's the story. Last week, the product manager dropped a requirement: we needed to fetch real-time market data from 200 sources, refresh every 10 seconds, and keep latency under 2 seconds. I looked at the existing code — synchronous requests fetching one by one. A full round took 14 seconds, CPU usage was below 5%, but all the time was wasted waiting on network I/O. The classic Python solution: asyncio. I spent an afternoon rewriting the core of the scraper. After deployment, QPS jumped from 20 to 500. The CTO stared at the monitoring dashboard for five minutes, then turned to ask if I had secretly added more machines. I said, "Nope, just changed a few dozen lines of Python."

Here, I'll share the core techniques, full code, and two deep pitfalls I fell into.


Your Python Spends Most of Its Time Waiting

Let's look at a typical scenario: fetching data from 10 URLs synchronously. The code might look like this:

import time
import requests

def fetch_sync(url: str) -> str:
    print(f"[{time.strftime('%X')}] 请求 {url}")
    resp = requests.get(url, timeout=5)
    return resp.text[:50]  # 截取前50字符示意

def main_sync():
    urls = [f"https://httpbin.org/delay/1?t={i}" for i in range(10)]
    start = time.perf_counter()
    results = [fetch_sync(url) for url in urls]
    elapsed = time.perf_counter() - start
    print(f"同步总耗时: {elapsed:.2f}s,数据条数: {len(results)}")

if __name__ == "__main__":
    main_sync()

Enter fullscreen mode Exit fullscreen mode

This code takes over 12 seconds because each request blocks sequentially, executing the 10 requests one after another. The CPU is basically sleeping — blocking I/O doesn’t consume CPU cycles, but the thread is stuck waiting.

Switch to asyncio: the slowest request decides the total time

The core idea of asyncio: single-threaded + event loop. When a coroutine starts waiting for a network response, it yields control back to the event loop, which immediately schedules the next coroutine. The total time is roughly the duration of the slowest request, not the sum of all requests.

Here’s the full comparison code (you can run it):

import asyncio
import time
import httpx   # httpx 提供原生 async 支持

# --- 异步版本 ---
async def fetch_async(client: httpx.AsyncClient, url: str) -> str:
    print(f"[{time.strftime('%X')}] 请求 {url}")
    resp = await client.get(url, timeout=5)
    return resp.text[:50]

async def main_async():
    urls = [f"https://httpbin.org/delay/1?t={i}" for i in range(10)]
    start = time.perf_counter()
    async with httpx.AsyncClient() as client:
        tasks = [fetch_async(client, url) for url in urls]
        results = await asyncio.gather(*tasks)
    elapsed = time.perf_counter() - start
    print(f"异步总耗时: {elapsed:.2f}s,数据条数: {len(results)}")

if __name__ == "__main__":
    asyncio.run(main_async())

Enter fullscreen mode Exit fullscreen mode

The result: the async version takes about 1.1 seconds, nearly the same as a single request's latency. That’s the power of asyncio.gather — it submits all coroutines to the event loop and lets the scheduler run them while waiting for I/O concurrently.

Advanced Tip: Limit Concurrency to Avoid Overwhelming the Server

In reality, you can’t have unlimited concurrency — upstream APIs have rate limits, or your machine has limited port resources. This is where asyncio.Semaphore comes in for concurrency control, for example, allowing only 20 requests at a time:

import asyncio
import httpx

CONCURRENCY = 20

async def fetch_with_limit(sem: asyncio.Semaphore, client: httpx.AsyncClient, url: str):
    async with sem:  # 超过 20 个协程时,其他会在这里等待
        resp = await client.get(url, timeout=5)
        return resp.status_code

async def main_limited():
    urls = [f"https://httpbin.org/delay/1?t={i}" for i in range(100)]
    sem = asyncio.Semaphore(CONCURRENCY)
    async with httpx.AsyncClient() as client:
        tasks = [fetch_with_limit(sem, client, url) for url in urls]
        results = await asyncio.gather(*tasks)
    print(f"完成 {len(results)} 个请求")

asyncio.run(main_limited())

Enter fullscreen mode Exit fullscreen mode

The semaphore works simply: each async with sem tries to acquire a permit. If 20 coroutines are already running, the new one is suspended until another coroutine finishes and releases the permit. This is asyncio’s native flow control, much more elegant than thread pools.

Lessons Learned: Two Traps That Each Cost Me an Hour

Trap 1: Calling synchronous requests.get inside a coroutine

When I first started migrating, I got lazy and directly wrote requests.get(url) inside an async def. The result: the entire event loop was blocked, and concurrency turned into serial execution. It took me a while to realize — never call synchronous blocking functions inside a coroutine. Either use async libraries (like httpx, aiohttp) or use loop.run_in_executor() to offload the blocking call to a thread pool:

# 临时方案:把同步调用丢

Enter fullscreen mode Exit fullscreen mode