Getting ready

We will use the following notation to work with coroutines:

import asyncio 

@asyncio.coroutine
def coroutine_function(function_arguments):
............
DO_SOMETHING
............

Coroutines use the yield from syntax introduced in PEP 380 (read more at https://www.python.org/dev/peps/pep-0380/) to stop the execution of the current computation and suspends the coroutine's internal state.

In particular, in the case of yield from future, the coroutine is suspended until future is done, then the result of future will be propagated (or raise an exception); in the case of yield from coroutinethe coroutine waits for another coroutine to produce a result that will be propagated (or raise an exception).

As we shall see in the next example, in which the coroutines will be used to simulate a finite state machine, we will use the yield from coroutine notation.

More on coroutines with asyncio are available at https://docs.python.org/3.5/library/asyncio-task.html.
..................Content has been hidden....................

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