【问题标题】:limit number of concurrent requests aiohttp限制并发请求的数量 aiohttp
【发布时间】:2018-05-05 14:24:23
【问题描述】:

我正在使用 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 获取)。我只是想知道我在这里做错了什么。

【问题讨论】:

    标签: python image python-requests python-asyncio aiohttp


    【解决方案1】:

    您的限制设置正常工作。你在调试时出错了。

    正如 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())
    

    【讨论】:

      【解决方案2】:

      asyncio.Semaphore 正好解决了这个问题。

      在你的情况下,它会是这样的:

      semaphore = asyncio.Semaphore(3)
      
      
      async def get_images(url, session):
      
          async with semaphore:
      
              print(f'Making request to {url}')
      
              # ...
      

      您可能也有兴趣查看演示信号量如何工作的可立即运行的代码example

      【讨论】:

      • 但是为什么在 TCP 连接器中设置限制不起作用?
      • @AndriyMaletsky 我猜它可以工作,但后来在执行流程中:在你的打印行之后,session.get 内部的某个地方。我认为不将这项工作委托给连接器更方便,而是在您想要的地方使用信号量。
      猜你喜欢
      • 2018-07-18
      • 2019-07-31
      • 2015-03-09
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多