Overview:
-
The method get_coro() returns the coroutine object wrapped by an asyncio.Task object.
-
In Python, a coroutine object cannot be reused once it has exited. The example below compares the coroutine object returned by the get_coro() and checks whether it is indeed the original coroutine object passed initially to the asyncio.create_task() function.
Example:
# Example Python program that retrieves tickerOn = True # Coroutine to be wrapped as an asyncio.Task # Coroutine that creates the asyncio.Task print("Making sure create_task() used the same coroutine instance...") except asyncio.CancelledError: # Create an event loop and run the task ticker1 = ticker() |
Output:
Making sure create_task() used the same coroutine instance... Result:True tick tick Ticker being stopped Ticker stopped... Ticker stopped. |