Python thread pool + into the city

Coding. 8 = UTF-# 
Import multiprocessing 
Import ThreadPool 
Import Threading 
Import Time 
Import OS 
Import Random 

the mutex of threading.Lock = () 


DEF write_to_file (file_name, text): 
    mutex.acquire () 
    A = Open (file_name, 'A') 
    A. Write (text + "\ n-") 
    a.close () 
    mutex.release () 



DEF test1 (MSG): 
    t_start the time.time = () 
    Print ( "% S started the process number% d"% (msg, os.getpid ())) 
    the time.sleep (random.random () * 2) 
    T_STOP the time.time = () 
    Print ( "% S execution is completed, time-consuming .2f%"% (MSG, T_STOP - t_start)) 




DEF multithread (parm_list, threading_num): 
    # here is stored into the thread pool parameters 
    # is put into the content list
    Content Order # call parameters and a list of functions is the same as 
    for i in parm_list: 
        password_list.append ((i, None)) 

    the pool = threadpool.ThreadPool (threading_num) 
    requests_a = threadpool.makeRequests (REQ, password_list) 
    [pool.putRequest (REQ) for REQ in requests_a] 
    pool.wait () 


DEF multiprocess (): 
    PO = multiprocessing.Pool (. 3) 
    for I in Range (0, 10): 
        . Pool # () apply_async (target to call, (transfer the parameters to the target Ganso,)) 
        child processes # each cycle will come out with your free to call the target 
        # for J in the Range () 
        po.apply_async (test1, (i,)) 

    Print ( "----- Start ----- ") 

    po.close () # close the process pool, closed after po no longer receive new request 
    after po.join () # wait for all po child process is completed, must be placed close statement

    print("-----end-----")


def main():
    pass

 

Guess you like

Origin www.cnblogs.com/yakoazz/p/11109434.html