Issue
Let's say we have
await async_function_one_with_large_IO_request()
await async_function_two_with_large_IO_request()
versus
asyncio.gather(
async_function_one_with_large_IO_request(),
async_function_two_with_large_IO_request())
In the first version, once we hit the 'large io request' part of function one, it's gonna move onto running function_two, that's the whole point of await
, right?
Isn't that what version 2 with gather does too?
What's the performance difference between the two?
Solution
In the first version, once we hit the 'large io request' part of function one, it's gonna move onto running function_two, that's the whole point of await, right?
That's incorrect. In your first version, async_function_two_with_large_IO_request
(which I will call function_two
) won't run until async_function_one_with_large_IO_request
(which I will call function_one
) completes.
If function_one
happens to await
on another function, it will yield control to another running async task, but function_two
hasn't been scheduled yet.
When you use asyncio.gather
, the tasks are scheduled concurrently, so if function_one
await
s on something, function_two
has a chance to run (along with other async tasks).
Note that asyncio.gather
creates an async task, which generally implies you have to await
on it:
await asyncio.gather(...)
The Python documentation covers this in detail in Coroutines and Tasks.
Answered By - larsks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.