We are all familiar with function which is also known as subroutine , procedure , subprocess , etc. . Function — it is a sequence of instructions packaged as a unit to perform a specific task. When the logic of a complex function is divided into several separate steps that are themselves functions, then these functions are called helper functions or routines .
Routines in Python are called by the main function, which is responsible for coordinating the use of these routines. Routines have a single entry point.
Coroutines are generalizations of subroutines. They are used for collaborative multitasking, where a process voluntarily gives control periodically or in standby mode to allow multiple applications to run concurrently. Difference between coroutine and subroutine:
- Unlike subroutines, coroutines have many entry points to pause and resume execution. A coroutine can suspend its execution and transfer control to another coroutine, and can resume execution where it left off.
- Unlike subroutines, there is no main function to call coroutines in a specific order and coordinate results. Coroutines are cooperative, which means they are chained together to form a pipeline. One coroutine can consume input and send to others who process it. Finally, there could be a coroutine to display the result.
Coroutine Vs Thread
Now you might be thinking how a coroutine is different from threads, both seem to do the same job.
In the case of threads, this is the operating system (or runtime) that switches between threads according to the scheduler. Although in the case of coroutines, it is the programmer and programming language that decides when to switch coroutines. Coroutines work together multitasking, pausing and resuming at specified points by the programmer.
In Python, coroutines are similar to generators, but with a few extra methods and a slight change in how we use the line = (yield) any value we send to the coroutine is captured and returned by (yield) .
The value can be sent to the coroutine using the send () method. For example, consider this coroutine, which prints out a name prefixed with "Dear". We will send the names to the coroutine using the send () method.
any value we send to the coroutine is captured and returned by (yield) .
Searching prefix: Dear Dear Atul
Execution of a coroutine is similar to a generator. When we call the coroutine, nothing happens, it runs only in response to the next () and send () methods. This is clearly seen in the example above, since only after calling the __next __ () method, our coroutine starts executing. After this call, execution jumps to the first yield expression, now execution pauses and waits for the value to be passed to the object. When the first value is sent to it, it checks the prefix and prints the name if the prefix is present. After printing the name, it loops through until it encounters the expression again.
Closing a coroutine
A coroutine can run indefinitely, using the close () . When the coroutine closes, it throws a GeneratorExit exception that can be caught in the usual way. After the coroutine is closed, if we try to send values, this will raise a StopIteration exception. Below is a simple example:
Searching prefix: Dear Dear Atul Closing coroutine !!
Chained coroutines to create a pipeline
Coroutines can be used to set pipes. We can chain coroutines together and send data through the pipe using the send () method. The pipe needs:
- Original source (manufacturer) that outputs the entire pipe. The producer is usually not a coroutine, it’s just a simple method.
- Sink which is the end point of the pipe. The sink can collect all the data and display it.
I’m sink, i’ll print tokens Searching for ing running moving Done with filtering !! Done with printing!
This article courtesy of Atul Kumar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.