Co-process in Python (co-program)

Source: Internet
Author: User
Tags call back

co-process: Write a function as a task that can handle input parameters

Use the yield statement and create a co-process in the form of an expression yield


def print_info (data): print (' Looking for ', data); While true:line = (yield) If the data in Line:print (line);

The above function is a coprocessor to use this function first call it and proceed to the first yield statement

info = print_info (' python '); info.__next__ (); #向前执行第一条yield语句

Output Result:
Looking for Python

Then use the Send method to send the data to the co-program for processing

Info.send (' Hell world '); Info.send (' This is Python '); Info.send (' Python goods ');

If the data parameter value is included in the sent message, the match is returned successfully

Output Result:
Looking for Python
This is Python
Python goods

Send () sends a value for the process sequence is in a temporary abort state when the value is sent, the yield expression returns this value, and the subsequent program processes the return value until the next expression ends, until the function returns or calls the Close method

Data generated based on part of the program is used by another part of the program (Producer-user mode)

When writing concurrent programs, the role of the coprocessor is obvious. He represents a consumer of data

Info =[print_info (' python '), print_info (' Hello '), Print_info (' Chunrui ')]

Prepare all the matches by calling __next__ ()

For n in info:n.__next__ ();

Define a function to get each column of data in a file and pass it to the generator

def tail (f): For line in F:if not Line:time.sleep (0.1); Continue #如果不存在 postpone 0.1s for next yield line;mylist = tail (open (' e:/work.txt '))

Loop the value in MyList and send to the coprocessor

For M in mylist:for N in Info:n.send (m);

Output Result:

Looking for Python
Looking for Hello
Looking for Chunrui

Python is Conputer language
Chunrui is Name
Hello world are the first case
I like to use Python
My name is Chunrui


1, Co -process: A task function that can handle input parameters when the process is paused we get from it to return worth call back to the program when the ability to pass in additional or new parameters can still continue from the last place left

2, use the Send function to send parameters for the process

This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source

Co-process in Python (co-program)

Related Article

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: 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.