Manipulating tasks with asyncio

The asyncio module is designed to handle asynchronous processes and concurrent task execution over an event loop. It also provides the asyncio.Task() class for the purpose of wrapping coroutines in a task (https://docs.python.org/3/library/asyncio-task.html). Its use is to allow independently running tasks to run concurrently with other tasks over the same event loop.

When a coroutine is wrapped in a task, it connects Task to the event loop and then runs automatically when the loop is started, thus providing a mechanism for automatically driving the coroutine.

The asyncio module provides the asyncio.Task(coroutine) method to handle computations with tasks; moreover, asyncio.Task(coroutine) schedules the execution of a coroutine (https://docs.python.org/3/library/asyncio-task.html).

A task is responsible for executing a coroutine object in an event loop.

If the wrapped coroutine uses the yields from future notation, as already described in the Handling coroutines with asyncio sectionthen the task suspends the execution of the wrapped coroutine and awaits completion of the future.

When the future is done, the execution of the wrapped coroutine restarts with the result or the exception of the future. Also, we must note that an event loop only runs one task at a time. Other tasks may run in parallel if other event loops are running in different threads.

While a task waits for the completion of a future, the event loop executes a new task.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.95.38