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:
- Process a begins execution.
- Process a executes to a step, suspends execution, and transfers execution to process B.
- The execution authority is returned after the execution of the process B for a period of time.
- 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:
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