Process Learning Notes

Source: Internet
Author: User

I. Introduction of the Association process

What is a co-process?

Co-process, also known as Micro-threading, threading, English name Coroutine. The process is a user-state lightweight thread

The co-process has its own register context and stack.

In short, the process is to switch back and forth, when encountering IO operations, such as reading and writing files, network operation, jumping to another thread to execute, and then encountered IO operation, and jumped back. Jumping over and over to perform, because it's fast, so it looks like it's executing concurrency, essentially a single thread.

Benefits of the co-process:

    1. No overhead for thread context switching
    2. No need for atomic operation locking and synchronization overhead
    3. Easy switching of control flow and simplified programming model
    4. High concurrency + high scalability + low cost
    5. A CPU that supports tens of thousands of threads is not a problem. So it's good for high concurrency processing.

Disadvantages of the co-process:

1. Inability to utilize multi-core resources

The nature of the process is single-threaded and cannot be used on multiple cores of a single CPU at the same time.
The process needs to work with processes to run on multiple CPUs, and most of the applications written on a daily basis are not necessary, except for CPU-intensive applications

2. Blocking (Blocking) operations (such as IO, read and write files, network) will block the entire program

Second,yield to achieve the simplest effect of co-process

Implementing concurrency Effects in a single thread

Take the simple producer consumer model for example, producer cooks make buns, consumers eat buns

Code Listing 1:

Description: 1 producers, do 1 buns at a time, do only 5, 2 consumers

1 Import Time2 ImportQueue3 4 defConsumer (name):5     Print("%s ready to start eating steamed buns ..."%name)6      whileTrue:7New_baozi =yield8         Print("[%s] eating bun%s"%(Name,new_baozi))9         #time.sleep (1)Ten  One defproducer (): AR = Con.__next__() -R = Con2.__next__() -n =0 the      whileN < 5:#producers make only 5 buns -n + = 1 -         Print("\033[32;1m[producer]\033[0m is ready for the new bun%s"%N) -Time.sleep (1) + con.send (n) - con2.send (n) +  A if __name__=='__main__': at     #definition of 2 consumers, one producer -con = consumer ("C1") -Con2 = Consumer ("C2") -p =producer () -  - #Show Results: in #C1 ready to start eating steamed buns ... - #C2 ready to start eating steamed buns ... to #[producer] made a new bun 1 + #[C1] is eating buns 1 - #[C2] is eating buns 1 the #[producer] made a new bun 2 * #[C1] is eating buns 2 $ #[C2] is eating buns 2Panax Notoginseng #[producer] made a new Bun 3 - #[C1] is eating buns 3 the #[C2] is eating buns 3 + #[producer] made a new Bun 4 A #[C1] is eating buns 4 the #[C2] is eating buns 4 + #[producer] made a new Bun 5 - #[C1] is eating buns 5 $ #[C2] is eating buns 5

Code Listing 2:

Description: Producers do only 5 buns, 2 per time, 2 consumers

1 Import Time2 ImportQueue3 4 defConsumer (name):5     Print("%s ready to start eating steamed buns ..."%name)6      whileTrue:7New_baozi =yield8         Print("[%s] eating bun%s"%(Name,new_baozi))9         #time.sleep (1)Ten  One defproducer (name): AR = Con.__next__() -R = Con2.__next__() -n =0 them =0 -Count =0 -      whileCount < 5:#producers do only 5 buns, 2 times a time -n = m + 1 +m = n + 1 -Count + = 1 +         Print("\033[32;1m chef%s\033[0m The new bun%s"%(name,n)) A         Print("\033[32;1m chef%s\033[0m The new bun%s"%(name,m)) atTime.sleep (1) - con.send (n) - con2.send (m) -  - if __name__=='__main__': -     #definition of 2 consumers, one producer incon = consumer ("Lily") -Con2 = Consumer ("quietly") top = producer ("Small white") +  - #Show Results: the #Lily is going to start eating steamed buns ... * #quietly ready to start eating steamed buns ... $ #chef Xiao Bai made a new bun 1Panax Notoginseng #chef Xiao Bai made a new bun 2 - #[Lily] is eating buns 1 the #[quietly] is eating buns 2 + #chef Xiao Bai made a new Bun 3 A #chef Xiao Bai made a new Bun 4 the #[Lily] is eating buns 3 + #[quietly] is eating buns 4 - #chef Xiao Bai made a new Bun 5 $ #chef Xiao Bai made a new bun 6 $ #[Lily] is eating buns 5 - #[quietly] is eating buns 6 - #chef Xiao Bai made a new bun 7 the #chef Xiao Bai made a new bun 8 - #[Lily] is eating buns 7Wuyi #[quietly] is eating buns 8 the #chef Xiao Bai made a new bun 9 - #chef Xiao Bai made a new bun Wu #[Lily] is eating buns 9 - #[quietly] is eating buns

Block with sleep, analog IO operation
One encounter blocking, how to throw the block to the operating system, yield can not be achieved, with the association to achieve

Three, Greenlet module

Greenlet, co-process module, is a third-party module, not python comes with, need to be installed to be able to use.
Import Module command: from Greenlet import Greenlet

code example:

1  fromGreenletImportGreenlet2 3 deftest1 ():4     Print(12)5 Gr2.switch ()6     Print(34)7 Gr2.switch ()8 9 deftest2 ():Ten     Print(56) One Gr1.switch () A     Print(78) -  -GR1 =Greenlet (test1) theGR2 =Greenlet (test2) - Gr1.switch () -  - #Show Results: + # A - # About + # the A # + at  - #Description: Execute test1 function First, print 12, switch to Test2 function, print 56, return to Test1 (where previously switched), print 34, switch to test, print - #the process is to switch back and forth
Greenlet Module Coprocessor Example

Write here first ...

Process Learning Notes

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.