Process pool, thread pool, coroutine

Process pool and thread pool

Open processes and threads need to consume resources, but a relative in comparison, the thread is relatively small, the use of computers in the computer maximum degree of layer

What is the pool

The maximum under safe conditions to ensure the use of computer hardware computer

In fact, the pool is to reduce the efficiency of the program, but to ensure the security of the computer (the development of hardware, software can not keep up speed)

Import ProcessPoolExecutor concurrent.futures from, HtreadPoolExecutor 
the pool = ProcessPoolExecutor (5) # open process pool 
# pool = HtreadPoolExecutor (5) # open thread pool 
DEF FUNC (): 
    i = 0 
    for i in the Range (10): 
        Print ( "FUNC ") 
        I + =. 1 
    return I 
DEF call_back (I): 
    Print (i.result ()) # Note, the results must be returned result () 
IF the __name__ == '__main__': 
    pool.submit (FUNC) .add_done_callback (call_back) # add tasks, using asynchronous callback, the function processing results into parentheses class 
    values # will automatically get passed as a parameter function

  

Note that the thread pool and open, process pool is the same. j will be replaced HtreadPoolExecutor it ProcessPoolExecutor

Coroutine

What I first summarize, process, thread, and then for the coroutine

Process; resource unit

Thread; execution units

So what do coroutine is born; single-threaded under concurrency, it is completely out of the programmer obscenity noun,

He said single-threaded concurrency, we would have to mention multi-channel technology

Multiplexed on time

Plus switching state of preservation

Multiplexing in space

A hardware device on a public computer

Programmers, on the code through their own testing procedures in io, io on the event by switching to the operating system code is an illusion that did not meet io, allowing the program to switch between running and ready state, to enhance the operation of the program effectiveness

 

Note that not switch plus save state will improve efficiency

We should be in two ways,

1io intensive

Improve efficiency

2 compute-intensive

Reduce efficiency

You can save the state, we first thought Builder

Finally, think of the yield

But the yield is io does not recognize, so we have to give up yield

We need to find a tool to identify io's (gevent module)

from gevent import monkey; monkey.patch_all () # long as coroutines we will use to sentence grammar, fixed format 
from gevent spawn Import 
Import Time 
DEF FUNC (): 
    the time.sleep (2) 
    Print ( "FUNC ") 
DEF index (): 
    the time.sleep (. 3) 
    Print (" index ") 
IF the __name__ == '__main__': 
    S1 = the spawn (FUNC) # the spawn this method, the write function name will automatically calls, 
    s2 = spawn (index) 
    s1.join () # wait, end all performed coroutine, the main thread will end 
    s2.join ()

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cherish937426/p/11360007.html