Coroutines in Python – Start and close coroutines example

In this Python tutorial, we will learn the basics of coroutines in Python with the code snippets.

All of us are familiar with functions in Python 3.x or earlier which is also known as a subroutine. A function is a sequence of instructions directed to perform a specific task. When the logic of a complex function is divided into several self-contained steps that can also be treated as functions, then these functions are called subroutines. Partitioning of a module which is known as modularity has a similar kind of approach.

Subroutines or partitioned functions in Python are with the help of main function which ensures interlinking of these subroutines. Subroutines have single entry point.

Also learn,

When we generalise subroutines they become Coroutines. They are used in the arena of cooperative multitasking where a process is forced to voluntarily yield the control.


  • The instructions of a program are executed for the first time at an entry point.
  • An entry point is a point where the program gains access to command line arguments.
  • The OS passes control to its entry point in order to start execution.

The difference between coroutine and subroutine in Python

  • Coroutines have many entry points for holding and resuming back the execution. A coroutine can hold an execution and then transfer control to other coroutines so that it can resume again execution from the point it left off.
  • There is no requirement of main function to make calls to coroutines in particular order and coordinate the results. Coroutines are highly cooperative. It means they link together to form a straight pathway. Any coroutine may consume input data and send it to others which may process it and then compute the result. Finally, there may any of the coroutine to display the result.

SOME INBUILT METHODS of coroutines in Python

To start the coroutine execution use syntax
<coroutine name>.__next__()

To send the inputs to coroutine use syntax
<coroutine name>.send(<input value>)

For closing a coroutine use syntax
<coroutine name>.close()

For taking the input with the help of yield statement use syntax

Python Code to execute coroutine

# Python3.x program 
# Execution of coroutine
def code_speedy(prefix): 
    print("Searching prefix:{}".format(prefix)) 
    while True: 
        line = (yield) 
        if prefix in line: 
#when we directly call coroutine, nothing will be displayed
corou = code_speedy("Code") 
# To start execution of coroutine and  
# Execution gets advanced to the first yield expression 
#use the intialisation statement
# Recieving inputs 
corou.send("Code Speedy")


Searching prefix:Welcome
Code Speedy

Python Code to Close a Coroutine

# Python3 program 
# closing a coroutine 
def print_name(prefix): 
    print("Searching prefix:{}".format(prefix)) 
    try :  
        while True: 
                name = (yield) 
                if prefix in name: 
    except GeneratorExit: 
            print("Coroutine closed succesfully!!") 
corou = print_name("Code") 
corou.send("Code Speedy") 


Searching prefix:Code
Code Speedy
Coroutine closed succesfully!!

Leave a Reply

Your email address will not be published. Required fields are marked *