From f9de9b05ceae0e59bbfd97bc83b7d04ee943e88e Mon Sep 17 00:00:00 2001 From: Mohit Date: Thu, 10 Oct 2024 10:32:09 +0530 Subject: [PATCH] Add example code for using asyncio.gather in Python's asyncio module --- docs/data_science_tools/python_snippets.md | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/docs/data_science_tools/python_snippets.md b/docs/data_science_tools/python_snippets.md index 583347e..dbac7ae 100644 --- a/docs/data_science_tools/python_snippets.md +++ b/docs/data_science_tools/python_snippets.md @@ -516,3 +516,61 @@ locale.getpreferredencoding = lambda: "UTF-8" + + +## Asyncio Gather + +- `asyncio.gather` is a powerful function in Python's `asyncio` module that allows you to run multiple coroutines concurrently and collect the results. Here is an example of how to use `asyncio.gather` to process a list of inputs concurrently and maintain order. + +```python linenums="1" +# import +import asyncio +import random + +# func to process input +async def process_input(input_value): + # Generate a random sleep time between 1 and 5 seconds + sleep_time = random.uniform(1, 5) + print(f"Processing {input_value}. Will take {sleep_time:.2f} seconds.") + + # Simulate some processing time + await asyncio.sleep(sleep_time) + + return f"Processed: {input_value}" + +async def main(): + # List of inputs to process + inputs = ["A", "B", "C", "D", "E"] + + # Create a list of coroutines to run + tasks = [process_input(input_value) for input_value in inputs] + + # Use asyncio.gather to run the coroutines concurrently and maintain order + results = await asyncio.gather(*tasks) + + # Print the results + for input_value, result in zip(inputs, results): + print(f"Input: {input_value} -> {result}") + +# Run the main function +if __name__ == "__main__": + asyncio.run(main()) +``` + +- Once you run the above code, here is an example of the output you might see: + +```shell +Processing A. Will take 2.45 seconds. +Processing B. Will take 1.47 seconds. +Processing C. Will take 4.47 seconds. +Processing D. Will take 1.68 seconds. +Processing E. Will take 4.47 seconds. +Input: A -> Processed: A +Input: B -> Processed: B +Input: C -> Processed: C +Input: D -> Processed: D +Input: E -> Processed: E +``` + +- As you can see from the output, the inputs are processed concurrently, and the results are collected in the order of the inputs. This is true even if the processing times are different for each input. +