Overview:
-
The run() method executes a coroutine on an asyncio event loop.
- Before executing a coroutine, the run() method validates whether the parameter passed is a valid coroutine object. Before executing the coroutine, the method makes sure that no event loop is already running.
- Unless a custom event loop is used, using the run() method is the recommended way for executing coroutines.
Example:
# Example Python program that executes # A simple coroutine that simulates collecting # A simple coroutine that simulates serving orders # A coroutine that awaits tasks # Execute the main coroutine through asyncio.run() |
Output:
Getting Order:1 Serving Order:1 Getting Order:2 Serving Order:2 Getting Order:3 Serving Order:3 Getting Order:4 Serving Order:4 Getting Order:5 Serving Order:5 Getting Order:6 Serving Order:6 Getting Order:7 Serving Order:7 Getting Order:8 Serving Order:8 Getting Order:9 Serving Order:9 Getting Order:10 Serving Order:10 |