我正在用Flask编写一个API,其中包含1000多个获取数据的请求,我想限制每秒的请求数。我尝试过:
conn = aiohttp.TCPConnector(limit_per_host=20)
and
conn = aiohttp.TCPConnector(limit=20)
但是似乎不起作用
我的代码如下:
import logging
import asyncio
import aiohttp
logging.basicConfig(filename="logfilename.log", level=logging.INFO, format='%(asctime)s %(levelname)s:%(message)s')
async def fetch(session, url):
async with session.get(url, headers=headers) as response:
if response.status == 200:
data = await response.json()
json = data['args']
return json
async def fetch_all(urls, loop):
conn = aiohttp.TCPConnector(limit=20)
async with aiohttp.ClientSession(connector=conn, loop=loop) as session:
results = await asyncio.gather(*[fetch(session, url) for url in urls], return_exceptions=True)
return results
async def main():
loop = asyncio.new_event_loop()
url_list = []
args = ['a', 'b', 'c', +1000 others]
urls = url_list
for i in args:
base_url = 'http://httpbin.org/anything?key=%s' % i
url_list.append(base_url)
htmls = loop.run_until_complete(fetch_all(urls, loop))
for j in htmls:
key = j['key']
# save to database
logging.info(' %s was added', key)
如果我运行代码,则在1秒内我发送了200多个请求。有什么方法可以限制请求?
上面的代码按预期工作(除了有关headers
未定义的小错误。
[在我的机器上经过测试,httpbin
URL大约在100毫秒内响应,这意味着并发20它将在1秒内处理大约200个请求(这也是您所看到的):
100 ms表示10个请求在一秒内完成每秒10个请求和并发20表示200个请求一秒钟]
限制选项(aiohttp.TCPConnector
)限制并发请求的数量,没有任何时间维度。
要查看实际限制,请尝试使用更多值,例如10
,20
,50
:
# time to complete 1000 requests with different keys
aiohttp.TCPConnector(limit=10): 12.58 seconds
aiohttp.TCPConnector(limit=20): 6.57 seconds
aiohttp.TCPConnector(limit=50): 3.1 seconds