asyncio
Concurrency, Not Parallelism:
asyncio
concurrent programming is single-process, single-threaded.

asyncio.gather
// main
CONCURRENT_TASKS = 4
semaphore = asyncio.Semaphore(CONCURRENT_TASKS)
tasks = [process_single_tab(..., semaphore) for ...]
await asyncio.gather(*tasks)
// process_single_tab
async with semaphore:
Asynchronized(异步)和Synchronised(同步)
异步就好像去邮局发信,你希望把信发到A家,你把信交给邮局工作人员就可以回家了,不用
等着A收到在回家,
同步是,你给A打电话,如果打通了,你一言我一语的就开始交流

单进程多线程Concurrency
比串行
的优势在哪里呢?
await
时,线程会让出控制权。那么在大量I/O 或者RPC调用时,只需要付出调用时间。
IO1 () {sleep(2); return; }
和async IO2 () {sleep(2); return; }
IO1
串行x5需要10s
asyncio
执行IO2
x5只需要2.01s