Questions And Answers

More Tutorials

Python Coroutines

Coroutines are mostly used in cases of time-consuming programs, such as tasks related to machine learning or deep learning algorithms, or in cases where the program has to read a file containing a large number of data. In such situations, we do not want the program to read the file or data again and again, so we use coroutines to make the program more efficient and faster. Coroutines run endlessly in a program because they use a while loop with a true or 1 condition, so it may run until infinite time. Even after yielding the value to the caller, it still awaits further instruction or calls. We have to stop the execution of the coroutine by calling the coroutine.close() function.

def myfunc():
    while True:
        value = (yield)

The while block continues the execution of the coroutine for as long as it receives values. The value is collected through the yield statement.

Coroutine Execution:-

Execution is the same as of a generator. When you call a coroutine, nothing happens. They only run in response to the next() and send() methods. Coroutine requires the use of the next statement first so it may start its execution. Without a next(), it will not start executing. We can search a coroutine by sending it the keywords as input using object name along with send(). The keywords to be searched are send inside the parenthesis.

When we run the next() function the first time, the coroutine executes and waits for new input. After the input is sent to it using the send() function, it executes it and again waits for next input, and the process goes on like this because we have set the while loop as true, so it will never exit its execution. In order to make the execution stop, we have to close the coroutine using coroutine.close() function.

send() — used to send data to coroutine
close() — to close the coroutine


def myfunc():
    while True:
        value = (yield)

coroutine =myfunc()
coroutine.send(" Tutorial ")



After closing the coroutine, if we send values, it will raise the StopIteration exception. Coroutines are used for data processing mechanisms. Coroutines are similar to generators, except they wait for information to be sent to it using send() function.


def searcher():
    import time
    # Some 4 seconds time consuming task
    book = "This is a book on harry and code with harry and good"

    while True:
        text = (yield)
        if text in book:
            print("Your text is in the book")
            print("Text is not in the book")

search = searcher()
print("search started")
print("Next method run")



In this page (written and validated by ) you learned about Python Coroutines . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Os Module.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.