Last active
June 8, 2024 06:42
-
-
Save vinayaksuresh/c1b6eeb09f71cb6df980d4fc9e425989 to your computer and use it in GitHub Desktop.
Series and Parallel calls via asyncio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import random | |
from time import time, sleep as _sleep | |
""" | |
This gist helps understand the basic implementation of the parallel and series call in python using the asyncio library. | |
The code demonstrates how to invoke asyncio functions and non-asyncio functions asynchronously. | |
Note from the below output - the parallel calls will always be completed in the ascending order of the sleep seconds, | |
where as the series call will be completed in the ascending order of the call id. | |
Sample Output: | |
Executing main with is_asyncio: True | |
Running series call 1 with sleep as 7 seconds | |
Running parallel call 4 with sleep as 9 seconds | |
Running parallel call 3 with sleep as 12 seconds | |
Running series call 2 with sleep as 8 seconds | |
Running parallel call 1 with sleep as 18 seconds | |
Running parallel call 2 with sleep as 18 seconds | |
Parallel Completed: [1, 2, 3, 4] | |
Running series call 3 with sleep as 12 seconds | |
Running series call 4 with sleep as 14 seconds | |
Running series call 5 with sleep as 9 seconds | |
Series Completed: [1, 2, 3, 4, 5] | |
Completed Main (is_asyncio=True) - 50.01380014419556 | |
******************************************************************* | |
Executing main with is_asyncio: False | |
Running series call 1 with sleep as 6 seconds | |
Running parallel call 2 with sleep as 9 seconds | |
Running parallel call 1 with sleep as 10 seconds | |
Running series call 2 with sleep as 5 seconds | |
Running parallel call 4 with sleep as 13 seconds | |
Running series call 3 with sleep as 3 seconds | |
Running parallel call 3 with sleep as 16 seconds | |
Parallel Completed: [1, 2, 3, 4] | |
Running series call 4 with sleep as 14 seconds | |
Running series call 5 with sleep as 3 seconds | |
Series Completed: [1, 2, 3, 4, 5] | |
Completed Main (is_asyncio=False) - 31.029510021209717 | |
******************************************************************* | |
""" | |
async def sleep(call_id, slug, is_asyncio=False): | |
random_num = random.randint(6, 20) - call_id | |
if is_asyncio: | |
await asyncio.sleep(random_num) | |
else: | |
loop = asyncio.get_event_loop() | |
await loop.run_in_executor( | |
None, | |
lambda: _sleep(random_num), | |
) | |
print(f"Running {slug} call {call_id} with sleep as {random_num} seconds") | |
return call_id | |
async def parallel_call(is_asyncio=False): | |
res = await asyncio.gather( | |
sleep(1, "parallel", is_asyncio), | |
sleep(2, "parallel", is_asyncio), | |
sleep(3, "parallel", is_asyncio), | |
sleep(4, "parallel", is_asyncio) | |
) | |
print("Parallel Completed:", res) | |
async def series_call(is_asyncio=False): | |
res = [] | |
res += [await sleep(1, "series", is_asyncio)] | |
res += [await sleep(2, "series", is_asyncio)] | |
res += [await sleep(3, "series", is_asyncio)] | |
res += [await sleep(4, "series", is_asyncio)] | |
res += [await sleep(5, "series", is_asyncio)] | |
print("Series Completed:", res) | |
async def main(is_asyncio): | |
start = time() | |
print (f"Executing main with is_asyncio: {is_asyncio}") | |
res = await asyncio.gather(parallel_call(is_asyncio), series_call(is_asyncio)) | |
print (f"Completed Main (is_asyncio={is_asyncio}) - {time() - start}") | |
print ("*******************************************************************") | |
return res | |
asyncio.run(main(is_asyncio=True)) | |
asyncio.run(main(is_asyncio=False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment