简体   繁体   English

使用信号量时如何存储来自 asyncio.gather() 的响应?

[英]How to store responses from asyncio.gather() when semaphore is used?

This is the code I'm trying to run:这是我要运行的代码:

import asyncio
from aiohttp import ClientSession


async def fetch(url, session):
    async with session.get(url) as response:
        data = await response.read()
        print(data)
        return data


async def bound_fetch(sem, url, session):
    async with sem:
        await fetch(url, session)


async def run(r):
    url = "http://localhost:8080"
    tasks = []
    sem = asyncio.Semaphore(1000)

    async with ClientSession() as session:
        for i in range(r):
            task = asyncio.ensure_future(bound_fetch(sem, url, session))
            tasks.append(task)

        responses = asyncio.gather(*tasks)
        await responses
        print(responses.result())

number = 10
loop = asyncio.get_event_loop()

future = asyncio.ensure_future(run(number))
loop.run_until_complete(future)

Output of the code: Output的代码:

b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
b'<!DOCTYPE html>\n<html>\n<body>\n\n<h1>Hello World</h1>\n\n</body>\n</html>'
[None, None, None, None, None, None, None, None, None, None]

My question is why does responses not contain the actual response of the requests?我的问题是为什么responses不包含请求的实际响应? Instead it is providing me with 'None' when actually I'm getting the expected response back during fetch()?相反,当我实际上在 fetch() 期间得到预期的响应时,它为我提供了“无”?

Your bound_fectch co-routine, used as an intermediary step to use the semaphore, does not return an explicit result (and therefore, implicitly, returns None).您的bound_fectch协同例程用作使用信号量的中间步骤,不返回显式结果(因此隐式返回 None)。

Just change it to:只需将其更改为:

async def bound_fetch(sem, url, session):
    async with sem:
        return await fetch(url, session)

(note the "return" keyword. There is no matter in it being inside the with block: exiting the function exists the block, n.netheless) (注意“return”关键字。在with块内没有任何问题:退出 function 存在块,n.netheless)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM