我跟进了这个教程:https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html,当我做50 000个请求时,一切正常.但我需要进行1百万个API调用,然后我对此代码有问题:
url = "http://some_url.com/?id={}" tasks = set() sem = asyncio.Semaphore(MAX_SIM_CONNS) for i in range(1,LAST_ID + 1): task = asyncio.ensure_future(bound_fetch(sem,url.format(i))) tasks.add(task) responses = asyncio.gather(*tasks) return await responses
@H_301_8@因为Python需要创建100万个任务,所以它基本上只是滞后,然后在终端中打印Killed消息.是否有任何方法可以使用预先制作的(或列表)网址的发生器?谢谢.
最佳答案
asyncio是内存绑定(与任何其他程序一样).你不能产生更多内存可以容纳的任务.我的猜测是你达到了内存限制.检查dmesg以获取更多信息.
1百万RPS并不意味着有1M个任务.任务可以在同一秒内执行多个请求.