Python coroutine usage Example Analysis

Source: Internet
Author: User
This article mainly introduces the usage of the python coroutine. The example analyzes the concept, functions, and usage of the Protocol in Python. If you need it, refer to the example in this article to describe the usage of the python coroutine. Share it with you for your reference. The details are as follows:

Write a function as a task to get a haircut and give it a series of input. This function is called a coroutine.

Def print_matchs (matchtext): print "looking for", matchtext while True: line = (yield) # create a coroutine if matchtext in line using the yield statement and the expression (yield: print line >>> matcher = print_matchs ('python') >>> matcher. next () looking for python >>> matcher. send ('Hello python') # check the generator. Difference Between send () and next (): hello python >>> matcher. send ('test') >>> matcher. send ('python is cool ') python is cool >>> matcher. close ()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.