httpx和aiohttp都是比较常用的异步请求库,当然requests+多线程或requests+gevent也是不错的选择。
一个使用httpx进行并发请求的脚本如下:
python
import functools
import sys
import time
import anyio
import httpx
async def fetch(client, results, index) -> int:
url = "https://qq.com"
results[index] = r = await client.get(url)
return len(r.content)
def timeit(func):
@functools.wraps(func)
async def deco(*args, **kw):
start = time.time()
rv = await func(*args, **kw)
print(f"{func.__name__} Cost: {round(time.time()-start,1)} seconds.")
return rv
return deco
@timeit
async def main():
if not sys.argv[1:]:
print(f'Usage::\n python {sys.argv[0]} 100\n')
return
total = int(sys.argv[1])
results = [None] * total
async with httpx.AsyncClient(follow_redirects=True, timeout=60) as client:
async with anyio.create_task_group() as tg:
for i in range(total):
tg.start_soon(fetch, client, results, i)
success = sum(map(bool, results))
print(f"{total = }; Success: {success}; Failed: {total-success}")
if __name__ == "__main__":
anyio.run(main)