Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await example question #4

Open
amejia1 opened this issue Sep 6, 2017 · 3 comments
Open

async/await example question #4

amejia1 opened this issue Sep 6, 2017 · 3 comments

Comments

@amejia1
Copy link

amejia1 commented Sep 6, 2017

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)
@dismantl
Copy link

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

@mjpieters
Copy link

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.

@TheQWERTYCodr
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants