本文主要是介绍使用协程库httpx并发请求,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
httpx和aiohttp都是比较常用的异步请求库,当然requests+多线程或requests+gevent也是不错的选择。
一个使用httpx进行并发请求的脚本如下:
import functools
import sys
import timeimport anyio
import httpxasync 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 rvreturn deco@timeit
async def main():if not sys.argv[1:]:print(f'Usage::\n python {sys.argv[0]} 100\n')returntotal = int(sys.argv[1])results = [None] * totalasync 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)
这篇关于使用协程库httpx并发请求的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!