Co-processes in Python

Source: Internet
Author: User

1 concept of co-process 1.1 co-process

Co-process, also known as micro-threading, fiber. English name Coroutine. One sentence explains what a thread is: The process is a lightweight thread that is user-state . (In fact, it does not indicate white ~)

I think that the association process, more abstract, if the thread has a certain understanding, it should be better understood.

  So it's easier to understand the process:

Threads are system-level, they are scheduled by the operating system, and the process is program-level, which is dispatched by the programmer on demand. We call one of the functions in a thread called subroutines, then the subroutine can be interrupted in the execution of the other subroutine, the other subroutine can also be interrupted back to continue to execute the previous subroutine, this is the process. That is, the same thread of code <1> execution can be interrupted, and then jump to execute another piece of code, when again back to execute the code block <1>, and then from the place where the previous break began.

  The comparative professional understanding is:

The co-process has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere, and the previously saved register context and stack are restored when it is cut back. Thus: The process can retain the state of the last invocation (that is, a specific combination of all local states), each time the procedure is re-entered, which is equivalent to the state of the last call, in other words: The position of the logical flow at the last departure.

1.2 Advantages and disadvantages of the co-process

Advantages of the co-process:

(1) without the overhead of thread context switching, the process avoids meaningless scheduling, which can improve performance (but therefore, programmers have to assume responsibility for scheduling, while the process also loses the ability of standard threads to use multiple CPUs)

(2) Cost of locking and synchronizing without atomic operation

(3) Easy to switch the control flow, simplify the programming model

(4) High concurrency + high scalability + Low cost: One CPU support for tens of thousands of processes is not a problem. Therefore, it is suitable for high concurrency processing.

Disadvantages of the co-process:

(1) Unable to take advantage of multi-core resources: The nature of the process is a single-threaded, it can not be a single CPU at the same time multiple cores, the process needs and processes to run on multi-CPU. Of course, most of the applications we write on a daily basis are not necessary, except for CPU-intensive applications.

(2) blocking (Blocking) operation (e.g. IO) blocks the entire program

2 How to implement the co-2.1 yield implementation in Python

As described earlier, "subroutines (functions) can be interrupted in the execution of other subroutines, and other subroutines can be interrupted to continue to execute the previous subroutine," then it is easy to think of Python yield, obviously yield can achieve this switch.

Example of using yield to implement a co-process:

 #!/usr/bin/env python #-*-coding:utf-8-*-defConsumer (name):Print("to start the Chew bones ...")       whileTrue:Print("\033[31;1m[consumer]%s\033[0m"%name) Bone=yield         Print("[%s] is chew bones%s"%(name, bone))defproducer (Obj1, OBJ2): Obj1.send (None)#start obj1 This generator, the first time must be with none <==> obj1.__next__ ()Obj2.send (None)#start Obj2 This generator, the first time must be with none <==> obj2.__next__ ()n =0 whileN < 5: N+ = 1Print("\033[32;1m[producer]\033[0m is producing bone%s"%N) obj1.send (n) obj2.send (n)if __name__=='__main__': Con1= Consumer ("Consumer a") Con2= Consumer ("Consumer B") producer (Con1, Con2)

Co-processes in Python

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.