You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`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
+
asyncdefprocess_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
+
returnf"Processed: {input_value}"
540
+
541
+
asyncdefmain():
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 inzip(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.
0 commit comments