__author__ = ‘admin‘ from gevent import monkey monkey.patch_all(thread=False) import gevent,time,os from threading import Thread,currentThread from multiprocessing import Process,Pool,current_process from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor def thread_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())) def process_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())) def gevent_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())) return item**2 def thread_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())) return item**2 def thread_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())) def process_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())) return item**2 def process_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())) def pool_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())) return item**2 def pool_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())) return item**2 if __name__ == ‘__main__‘: # 开启单纯进程、线程、协成的方法 def thread(value): threads = [] for i in range(value): t = Thread(target=thread_fun,args=(i,)) threads.append(t) t.start() for obj in threads: obj.join() print(‘--------->thread is end‘) def process(value): processs = [] for i in range(value): p = Process(target=process_fun,args=(i,)) processs.append(p) p.start() for obj in processs: obj.join() print(‘------>process is end‘) def gev(value): Gevents = [] for i in range(value): g = gevent.spawn(gevent_fun,i) Gevents.append(g) gevent.joinall(Gevents) res = [obj.value for obj in Gevents] print(res) print(‘------>gevent is end‘) def thread_pool(value): thp = ThreadPoolExecutor(4) targets = [] for i in range(value): target = thp.submit(thread_pool_fun,i) # target = thp.submit(thread_pool_fun,i).add_done_callback(thread_pool_fun_callback) #这种方式使用回调函数,就不能再获取函数(第一个、第二个)的返回值,如果使用result()会报错,报错原因是没有result属性 targets.append(target) thp.shutdown() res = [obj.result() for obj in targets] # obj = thp.map(thread_pool_fun,range(5)) # res = list(obj) print(res) print(‘------>ThreadPoolExecutor is end‘) def process_pool(value): pop = ProcessPoolExecutor(4) targets = [] for i in range(value): target = pop.submit(process_pool_fun,i) # target = pop.submit(process_pool_fun,i).add_done_callback(process_pool_fun_callback) # 这种方式使用回调函数,就不能再获取函数(第一个、第二个)的返回值,如果使用result()会报错,报错原因是没有result属性 targets.append(target) pop.shutdown() res = [obj.result() for obj in targets] # obj = pop.map(process_pool_fun,range(value)) # res = list(obj) print(res) print(‘------>ProcessPoolExecutor is end‘) def pool(value): targets = [] p = Pool(4) for i in range(value): target = p.apply_async(func=pool_fun,args=(i,)) # target = p.apply_async(func=pool_fun,args=(i,),callback=pool_fun_callback) # 注意一点的是,在回调函数下使用get()方法取得的结果仍然是第一个函数处理后的结果 targets.append(target) p.close() p.join() res = [obj.get() for obj in targets] # obj = p.map(pool_fun,range(5)) # obj = p.map_async(pool_fun,range(5)) # map()函数的使用 # res = list(obj)#第一种map的获取结果方式,是个列表 # res = obj.get()#第二种map的获取结果方式 print(res) print(‘------>processpool is end‘) #开启组合多进程、线程、协成的方法 def run(process_num,thread_num,gevent_num): t_pool = ThreadPoolExecutor(4) p_pool = ProcessPoolExecutor(4) processs = [] threads = [] gevents = [] for i in range(process_num): obj = p_pool.submit(process_pool_fun,i) processs.append(obj) for i in range(thread_num): obj = t_pool.submit(thread_pool_fun,i) threads.append(obj) for i in range(gevent_num): obj = gevent.spawn(gevent_fun,i) gevents.append(obj) p_pool.shutdown() t_pool.shutdown() gevent.joinall(gevents) p_result = [item.result() for item in processs] t_result = [item.result() for item in threads] g_value = [item.value for item in gevents] print(‘<p_result:%s>‘%p_result) print(‘<t_result:%s>‘%t_result) print(‘<g_value:%s>‘%g_value) 调用单纯开启进程、线程、协成的方式 thread(5) process(5) gev(5) thread_pool(5) process_pool(5) pool(5) 调用混合开启进程、线程、协成的方式 run(5,5,5)
原文地址:https://www.cnblogs.com/52-qq/p/8282416.html
时间: 2024-10-28 18:59:13