requests-threads icon indicating copy to clipboard operation
requests-threads copied to clipboard

async/await example question

Open amejia1 opened this issue 8 years ago • 3 comments

I'm not that familiar with this new AsyncSession object, but it looks to me like the async/await example doesn't actually perform those GET requests concurrently as you are awaiting for each one to return one by one. Should that example be this instead?

async def _main():
    for future in asyncio.as_completed([session.get('http://httpbin.org/get') for _ in range(100)]):
        print(await future)

amejia1 avatar Sep 06 '17 14:09 amejia1

This is correct. Looking at a wireshark dump, I can confirm the async/await example sends the requests sequentially rather than concurrently.

When trying the suggested replacement function above however, I get the following error: builtins.AssertionError: yield from wasn't used with future

dismantl avatar Mar 13 '18 21:03 dismantl

The session.request() method returns twisted Deferred objects; these can't really be treated as futures. You'd want to put all the deferreds into a DeferredList, I think. I'm not that versed in Twisted and their asyncio integration.

mjpieters avatar Sep 06 '19 15:09 mjpieters

you can just append the deferreds to a standard list and then await them all in a for loop. the for loop ends at roughly the same time as the slowest task, and is only slowed down by overhead from Python.

TheQWERTYCodr avatar Dec 04 '22 03:12 TheQWERTYCodr