限制并发请求数aiohttp

2024-05-21 08:53:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)))

问题是,我抛出了一个print语句来显示每个请求是什么时候发出的,它一次发出了几乎21个请求,而不是我想要限制它的3个(即,一旦一个图像下载完毕,它就可以转到列表中的下一个url来获取)。我只是想知道我做错了什么。在


Tags: toloopurlsizegetasyncaiohttprequest
2条回答

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

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

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

你的情况是这样的:

semaphore = asyncio.Semaphore(3)


async def get_images(url, session):

    async with semaphore:

        print(f'Making request to {url}')

        # ...

您还可能有兴趣看看这个演示信号量如何工作的现成代码example。在

相关问题 更多 >