Play Python (6) Co-process

Source: Internet
Author: User

Multitasking systems generally need to solve a problem: How multiple tasks are scheduled. Preemptive scheduling is a very common task scheduling mechanism. In the single-core mode of process scheduling as an example, a process is running, the other is in the ready queue, until the current running process to abandon the use of the CPU, the system will immediately assign the CPU to the newly arrived process, because the order of execution of the task is not deterministic, it looks like a bunch of tasks in the competition CPU use, Therefore, this multi-tasking mode is called "Multi-Task competition". The corresponding non-preemptive scheduling. The current task continues until the CPU is actively discarded for some reason, and the order of execution for each task is determined, as if it were collaborating, so this multi-tasking operation is also called "multitasking collaboration." Co-process is a typical multi-tasking collaboration solution. A process is a procedure that works with the caller to produce the value provided by the caller. It operates in the following general process:

    1. Process a begins execution.
    2. Process a executes to a step, suspends execution, and transfers execution to process B.
    3. The execution authority is returned after the execution of the process B for a period of time.
    4. Process a resumes execution.

So how does the generator relate to the co-process? In a generator, yield item This line of code yields a value to the caller of next () and, in addition, compromises the execution of the generator, allowing the caller to continue working until another value is needed to invoke next (). The caller pulls the value from the generator. If you can only produce values, the generator is obviously of little use. In the version after python2.5, the Send () method is added to the generator API. The caller of the generator can send data using the Send () method, and the sent data becomes the value of the yield expression in the generator function. This allows the generator to receive both data and output data, which can be used as a co-process. Here is a simple example of a co-process:

def fun():    r = ‘‘    while True:        n = yield r        if not n:            return        print(‘fun test %s...‘ % n)        r = ‘200 OK‘

When the process executes to the expression n = yield r, it first outputs R, and then assigns the received data to N. It then executes down until it encounters the next yield statement, which repeats itself. Now let's see how this process works.

c = fun()next(c)c.send(‘1‘)

The test results are as follows:

input:1

Although there is a dead loop in fun (), when yield is met, the co-process will give up the power of execution. And the process has a bright spot, it does not involve context switching, all processes are in the user space, the creation of the co-operation is also very small, so there is no way to create a thread at all considerations. So if there are multiple tasks, and we know how to schedule task collaboration, the process is undoubtedly a good choice.

Play Python (6) Co-process

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