The python thread pool concurrency

Use the thread pool to achieve high IO concurrency

模块:ThreadPoolExecutor, as_completed

Test code is as follows:

! # / opt / to python3 / bin / to python3 

from the ThreadPoolExecutor concurrent.futures Import, as_completed 
Import Time 

DEF Test (arg1, arg2, arg3): 
    the time.sleep (int (arg1)) 
    Print ( 'Parameter 1:% s 2 parameters: parameters% s. 3:% s'% (arg1, arg2, arg3)) 
    return arg1 


# Create a thread pool having 3 threads 
with the ThreadPoolExecutor (. 3) AS Executor: 
    # generate all tasks 
    all_task = [executor.submit (test, ag1 , ag2, ag3) for ag1, ag2, ag3 in [( '2', 'aa1', 'aa2'), ( '3', 'bb1', 'bb2')]] 

# after waiting task executed completely, the result for the use of the method loops back the results 
for OUT in as_completed (result): 
    Mess = out.result () 
    Print (Mess) 

# as_completed task is to wait for the result of all finished 
# result is to return to the task of extraction results

  

Guess you like

Origin www.cnblogs.com/zy6103/p/10963445.html