python – 限制并发请求数aiohttp
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 限制并发请求数aiohttp,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2010字,纯文字阅读大概需要3分钟。
内容图文
![python – 限制并发请求数aiohttp](/upload/InfoBanner/zyjiaocheng/820/6a2a6b95d275445a962e5d9d96ac3e07.jpg)
我正在使用aiohttp下载图像,并想知道是否有办法限制尚未完成的打开请求的数量.这是我目前的代码:
async def get_images(url, session):
chunk_size = 100
# Print statement to show when a request is being made.
print(f'Making request to {url}')
async with session.get(url=url) as r:
with open('path/name.png', 'wb') as file:
while True:
chunk = await r.content.read(chunk_size)
if not chunk:
break
file.write(chunk)
# List of urls to get images from
urls = [...]
conn = aiohttp.TCPConnector(limit=3)
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession(connector=conn, loop=loop)
loop.run_until_complete(asyncio.gather(*(get_images(url, session=session) for url in urls)))
问题是,我抛出一个打印声明,告诉我何时发出每个请求,并且它一次发出近21个请求,而不是我想要限制它的3个(即,一旦图像完成下载,它可以移动到列表中的下一个URL来获取).我只是想知道我在这里做错了什么.
解决方法:
您的限制设置正常.你在调试时弄错了.
正如Mikhail Gerasimov在the comment中指出的那样,你将print()调用放在错误的位置 – 它必须在session.get()上下文中.
为了确保限制得到尊重,我针对简单的日志记录服务器测试了您的代码 – 测试显示服务器正好接收您在TCPConnector中设置的连接数.这是测试:
import asyncio
import aiohttp
loop = asyncio.get_event_loop()
class SilentServer(asyncio.Protocol):
def connection_made(self, transport):
# We will know when the connection is actually made:
print('SERVER |', transport.get_extra_info('peername'))
async def get_images(url, session):
chunk_size = 100
# This log doesn't guarantee that we will connect,
# session.get() will freeze if you reach TCPConnector limit
print(f'CLIENT | Making request to {url}')
async with session.get(url=url) as r:
while True:
chunk = await r.content.read(chunk_size)
if not chunk:
break
urls = [f'http://127.0.0.1:1337/{x}' for x in range(20)]
conn = aiohttp.TCPConnector(limit=3)
session = aiohttp.ClientSession(connector=conn, loop=loop)
async def test():
await loop.create_server(SilentServer, '127.0.0.1', 1337)
await asyncio.gather(*(get_images(url, session=session) for url in urls))
loop.run_until_complete(test())
内容总结
以上是互联网集市为您收集整理的python – 限制并发请求数aiohttp全部内容,希望文章能够帮你解决python – 限制并发请求数aiohttp所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。