__author__='Admin' fromGeventImportMonkeymonkey.patch_all (thread=False)ImportGevent,time,os fromThreadingImportThread,currentthread fromMultiprocessingImportprocess,pool,current_process fromConcurrent.futuresImportProcesspoolexecutor,threadpoolexecutor
defThread_fun (item):Print('\033[33m <name:%s> <value:%s> <pid:%s> start ...'%(CurrentThread (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[33m <name:%s> <value:%s> <pid:%s> end ...'%(CurrentThread (). Name,item,os.getpid ()))
defProcess_fun (item):Print('\033[35m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[35m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ()))
defGevent_fun (item):Print('\033[36m <name:%s> <value:%s> <pid:%s> start ...'%(CurrentThread (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[36m <name:%s> <value:%s> <pid:%s> end ...'%(CurrentThread (). Name,item,os.getpid ()))returnItem**2
defThread_pool_fun (item):Print('\033[32m <name:%s> <value:%s> <pid:%s> start ...'%(CurrentThread (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[32m <name:%s> <value:%s> <pid:%s> end ...'%(CurrentThread (). Name,item,os.getpid ()))returnItem**2
defThread_pool_fun_callback (item):Print('\033[31m <name:%s> <value:%s> <pid:%s> start ...'%(CurrentThread (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[31m <name:%s> <value:%s> <pid:%s> end ...'%(CurrentThread (). Name,item,os.getpid ()))
defProcess_pool_fun (item):Print('\033[30m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[30m <name:%s> <value:%s> <pid:%s> end ...'%(Current_process (). Name,item,os.getpid ()))returnItem**2
defProcess_pool_fun_callback (item):Print('\033[29m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[29m <name:%s> <value:%s> <pid:%s> end ...'%(Current_process (). Name,item,os.getpid ()))
defPool_fun (item):Print('\033[28m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[28m <name:%s> <value:%s> <pid:%s> end ...'%(Current_process (). Name,item,os.getpid ()))returnItem**2
defPool_fun_callback (item):Print('\033[33m <name:%s> <value:%s> <pid:%s> start ...'%(Current_process (). Name,item,os.getpid ())) Time.sleep (1) Print('\033[33m <name:%s> <value:%s> <pid:%s> end ...'%(Current_process (). Name,item,os.getpid ()))returnItem**2
if __name__=='__main__': #open processes, threads, and co-forming methods defThread (value): Threads= [] forIinchRange (value): T= Thread (target=thread_fun,args=(i,)) Threads.append (t) t.start () forObjinchThreads:obj.join ()Print('--------->thread is end') defprocess (value): Processs= [] forIinchRange (value): P= Process (target=process_fun,args=(i,)) Processs.append (P) p.start () forObjinchProcesss:obj.join ()Print('------>process is end') defGeV (value): Gevents= [] forIinchRange (value): G=gevent.spawn (gevent_fun,i) gevents.append (g) Gevent.joinall (gevents) Res= [Obj.value forObjinchGevents]Print(RES)Print('------>gevent is end') defThread_pool (value): THP= Threadpoolexecutor (4) Targets= [] forIinchRange (value): Target=thp.submit (thread_pool_fun,i)#target = Thp.submit (thread_pool_fun,i). Add_done_callback (Thread_pool_fun_callback) #using the callback function in this way, you can no longer get the return value of the function (the first, second), if you use result (), the error is because there is no result attributetargets.append (target) Thp.shutdown () Res= [Obj.result () forObjinchtargets]#obj = Thp.map (Thread_pool_fun,range (5)) #res = list (obj) Print(RES)Print('------>threadpoolexecutor is end') defProcess_pool (value): Pop= Processpoolexecutor (4) Targets= [] forIinchRange (value): Target=pop.submit (process_pool_fun,i)#target = Pop.submit (process_pool_fun,i). Add_done_callback (Process_pool_fun_callback) #using the callback function in this way, you can no longer get the return value of the function (the first, second), if you use result (), the error is because there is no result attributetargets.append (target) Pop.shutdown () Res= [Obj.result () forObjinchtargets]#obj = Pop.map (process_pool_fun,range (value)) #res = list (obj) Print(RES)Print('------>processpoolexecutor is end') defPool (value): Targets=[] P= Pool (4) forIinchRange (value): Target= P.apply_async (func=pool_fun,args=(i,))#target = P.apply_async (func=pool_fun,args= (i,), Callback=pool_fun_callback) #Note that the result of using the Get () method under a callback function is still the result of the first function being processedtargets.append (target) p.close () P.join () Res= [Obj.get () forObjinchtargets]#obj = P.map (Pool_fun,range (5)) #obj = P.map_async (Pool_fun,range (5)) #use of the map () function #res = list (obj) #第一种map的获取结果方式, is a listing #res = Obj.get () #第二种map的获取结果方式 Print(RES)Print('------>processpool is end') #turn on the combination of multi-process, threading, and co-forming methods defRun (process_num,thread_num,gevent_num): T_pool= Threadpoolexecutor (4) P_pool= Processpoolexecutor (4) processs=[] Threads=[] gevents= [] forIinchRange (process_num): obj=p_pool.submit (process_pool_fun,i) processs.append (obj) forIinchRange (thread_num): obj=t_pool.submit (thread_pool_fun,i) threads.append (obj) forIinchRange (gevent_num): obj=gevent.spawn (gevent_fun,i) gevents.append (obj) p_pool.shutdown () T_pool.shutdown () GE Vent.joinall (gevents) P_result= [Item.result () forIteminchProcesss] T_result= [Item.result () forIteminchThreads] G_value= [Item.value forIteminchGevents]Print('<p_result:%s>'%P_result)Print('<t_result:%s>'%T_result)Print('<g_value:%s>'%g_value)call to open process, thread, and co-forming mode simply Thread (5) process (5) GeV (5) Thread_pool (5) Process_pool (5) Pool (5)call a hybrid open process, thread, and co-formed mode Run (5,5,5)
process, thread, and co-open mode