Skip to content

Commit e99e3fb

Browse files
authored
Merge pull request #36 from imohitmayank/mohit-10102024-updates
Add example code for using asyncio.gather in Python's asyncio module
2 parents cd68366 + f9de9b0 commit e99e3fb

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/data_science_tools/python_snippets.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,61 @@ locale.getpreferredencoding = lambda: "UTF-8"
516516
<!-- ## Python Classmethod vs Staticmethod
517517
518518
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner -->
519+
520+
521+
## Asyncio Gather
522+
523+
- `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.
524+
525+
```python linenums="1"
526+
# import
527+
import asyncio
528+
import random
529+
530+
# func to process input
531+
async def process_input(input_value):
532+
# Generate a random sleep time between 1 and 5 seconds
533+
sleep_time = random.uniform(1, 5)
534+
print(f"Processing {input_value}. Will take {sleep_time:.2f} seconds.")
535+
536+
# Simulate some processing time
537+
await asyncio.sleep(sleep_time)
538+
539+
return f"Processed: {input_value}"
540+
541+
async def main():
542+
# List of inputs to process
543+
inputs = ["A", "B", "C", "D", "E"]
544+
545+
# Create a list of coroutines to run
546+
tasks = [process_input(input_value) for input_value in inputs]
547+
548+
# Use asyncio.gather to run the coroutines concurrently and maintain order
549+
results = await asyncio.gather(*tasks)
550+
551+
# Print the results
552+
for input_value, result in zip(inputs, results):
553+
print(f"Input: {input_value} -> {result}")
554+
555+
# Run the main function
556+
if __name__ == "__main__":
557+
asyncio.run(main())
558+
```
559+
560+
- Once you run the above code, here is an example of the output you might see:
561+
562+
```shell
563+
Processing A. Will take 2.45 seconds.
564+
Processing B. Will take 1.47 seconds.
565+
Processing C. Will take 4.47 seconds.
566+
Processing D. Will take 1.68 seconds.
567+
Processing E. Will take 4.47 seconds.
568+
Input: A -> Processed: A
569+
Input: B -> Processed: B
570+
Input: C -> Processed: C
571+
Input: D -> Processed: D
572+
Input: E -> Processed: E
573+
```
574+
575+
- 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.
576+

0 commit comments

Comments
 (0)