Python Part IV notes _ _ _ higher order programming processes, threads, coroutines _4. Coroutine

1. coroutine concept :

  Routine or subroutine, all call hierarchy in all languages, such as A calls B, B then process execution and can call C, C finished return, execution returns B, A is finally finished return. Is achieved through the stack, a thread is to perform a self-proclaimed, claiming to call an entry, a return to order call is clear.

  Code:

def C():
    print("C--Start")
    print("C--end")

def B():
    print("B--Start")
    C()
    print("B--end")

def A():
    print("A--Start")
    B()
    print("A--end")

A()

# A--Start
# B--Start
# C--Start
# C--end
# B--end
# A--end

   Coroutine: it looks like it's routine, but in the implementation process can be interrupted within the subroutine. Then turn to the implementation of other interrupt subroutine without function calls.

  Coroutine compared with the thread, coroutine execution efficiency is extremely high, because only one thread, there is no write collision of variables, leading to resource coordination process in an unlocked only need to determine the state of it. For example, the following code, how to deal with?

DEF A (): 
    Print ( . 1 ) 
    Print ( 2 ) 
    Print ( . 3 ) 

DEF B (): 
    Print ( " X " ) 
    Print ( " Y " ) 
    Print ( " Z " ) 

# execution result without A calls B 
# . 1    2    XYZ     . 3 
# look a, B during the execution of a bit like a thread, but then in a characteristic coroutine execution thread

 

 

2. Establish a simple coroutine :

  Coroutine is achieved by a generator.

  First on the code:

RUN DEF (): 
    Print ( . 1 )
     the yield  10 
    Print ( 2 )
     the yield  20 is 
    Print ( . 3 )
     the yield  30 

# coroutine simple style, the control function of the stage of implementation, saving thread or process switching. 
# Return value is a generator. 
m = RUN () 
Print (Next (m))

  Description: In fact, we here use the concept of a generator. Trigger a generator so that the generator continued to perform.

  Description: yield = return, both literally means the same, but the yield is not directly return to return, but to wait for a user operation, let it return to a coupling position.

  

 3. Data transmission :

  Code:

DEF RUN (): 
    # null variables stored role data has not empty 
    data = "" 

    R & lt = the yield data 
    # R & lt = A 
    Print ( . 1 , R & lt, data) 

    R & lt = the yield data 
    # R & lt = B 
    Print ( 2 , R & lt, Data) 

    R & lt = the yield Data 
    # R & lt = C 
    Print ( . 3 , R & lt, Data) 

    R & lt = the yield Data 


m = RUN () is equal to # produced a generator 
# start m 
Print (m.send (None)) 
Print (m.send ( " A " )) 
Print (m.send ( " B " )) 
Print (m.send ( " C " )) 

# Note that this is a null value print 
# . 1 A 
# 
# 2 B 
# 
# 3 c

 

   Description 1: .send () message is sent to the generator.

  Note 2: run as follows :( note that we should yield seen as a return like to understand)

      Step: None give function sends a null value, the null value after receiving, the yield returns NULL data to r, the print empty.

      Part II: We give a transmission data "a", and then returns the data to yield "a" to the value of r, the print 1, "a"

      Similar behind.

   

  For example: We use the form of a function of the parameters passed to the generator.

Producer DEF (C): 
    c.send (None) 
    for I in Range ( . 5 ): 
        Print ( " producer generates data D% " % I) 
        R & lt = c.send (STR (I)) 
        Print ( " consumer spending data S% " % R & lt) 
    c.close () 


DEF customer (): 
    data = " " 
    the while True: 
        n- = the yield data
         IF Not n-:
             return 
        Print ( " consumer spending of S% " %  n-)
        data= " 200 " 



c = the Customer () 
Producer (c) 
# 0 producers generate data 
# consumer spending 0 
# consumer spending data 200 
# 1 producer generates data 
# 1 consumer spending 
# consumer spending data 200 
# 2 producer generates data 
# 2 consumer spending 
# 200 consumer spending data 
# 3 producer generates data 
# 3 consumer spending 
# 200 consumer spending data 
# 4 producers generate data 
# consumer spending up 4 
# 200 consumer spending data

 

Guess you like

Origin www.cnblogs.com/noah0532/p/10939313.html