A function is also known as a subprocess, procedure, subroutine, and many other names. A sequence of instructions pack into a unit to complete a specific task is known as a function. So, in the case of complex functions, it is divide into several self-contain steps; these steps are themselves functions. Such functions are known as helper functions or subroutines in python for data science.
The main function is responsible for coordinating the use of subroutines. The main function also calls upon the subroutines. So, the subroutines have a single entry point.
Now coming to coroutines, they are a generalize form of subroutines. Coroutines are use in the case of cooperative multitasking. In this process voluntarily yields control or when it is idle in order to enable multiple apps to run simultaneously.
Let’s look at the major differences between coroutine and subroutine:
- Coroutines have multiple entry points, unlike subroutines for the suspension and redemption of execution. Coroutine houses the feature of suspending its execution process and delegating the control to any other coroutine. It also resume its execution from the place it left control.
- In coroutines, we don’t have a main function to call upon coroutines. Coroutines work in cooperation and form a link. In the link one of the coroutines may perform the consumption of input data, another may process it, and a third one may be responsible for the display of results.
Difference between Coroutine and Thread:
At first glance, it seems both thread and coroutine are similar, but this is not the case. When we speak about threads, the operating system switches between threads according to the scheduler. But when we come to coroutines, the programmer and the program are responsible for deciding when to switch between coroutines.
Coroutine in Python:
There is a similarity between coroutines and generators in Python. But there are some extra methods and a slight change in the use of yield statements. While generators produce data for iteration, coroutines are also able to consume data.
A slight modification was done in Python 2.5 to the yield statement. It was also usable as an expression.
Values sent to coroutine are capture and returne by (yield) expression. To send values to a coroutine send() method is use.
Execution process is similar to the generator in coroutines. Coroutines only respond to next() and send() method. In the above example only after calling the next() method the execution starts. Then it advances to first yield expression. Here the execution comes to a halt, and it waits for the transfer of value to corou object. After the first value is sent, prefix check is done and if prefix is present print name is executed. After the printing of name, the loop is follow until it encounters name = (yield) expression again.
Closing a Coroutine:
For closing, a coroutine close() method is use. During the closure of coroutine GeneratorExit exception is generated. If we try to send values after closing, then, StopIteration exception will be raised. Let us look into an example:
Chaining of Coroutines for Creation of Pipelines:
Coroutines is use for setting up of pipes and pushing data through them. It is done using send() method. There are some requirements for a pipe.
- First of all, an initial source or producer is required. The producer is not a coroutine but a simple method.
- Next we require a sink. It is present at the endpoint of a pipe. It collects all data and display the same.
Let’s look into a simple example of chaining: