Multithreading one case

from concurrent.futures import ThreadPoolExecutor # Import thread pool 
from threading import current_thread # Import view the current thread from the thread method 
Import Time, Random 

the pool = the ThreadPoolExecutor (2) 


DEF Task (I): 
    A = the random.randint (0,5) 
    the time.sleep (A) 
    Print ( "2222working ...") 
    return i ** i 
objs = [] 
for i in the Range (1, 10): 
    res_obj = pool.submit (task, i) # asynchronously submit the task, returns an object used to represent the results of the task, namely pool.submit () will generate objects 
    objs.append (res_obj) 

# pool.shutdown (the wait = True) # the shutdown is a blocking function, 
# refers to the process can not go down to the pool submit the task, wait = True wait for the process thread pool or pool all tasks are finished running, 
# allowed to proceed down the code. 

# Out the results from the result object 
for res_obj in objs: # out objects from a list
    print (res_obj.result ()) # submit the objects have obtained result () method, i.e., the object encapsulates the return value 
Print (len (OBJS)) 
Print ( "over")

  

Guess you like

Origin www.cnblogs.com/realadmin/p/12071065.html