进程、线程、协程开启方式
2023-09-14 08:57:25 时间
__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)
相关文章
- Windows技术篇——进程、线程、消息机制进程间通信[通俗易懂]
- 进程与线程
- Android进程与线程
- [Linux]C语言Linux系统编程创建进程详解编程语言
- Linux进程间通信技术探索(linux进程的通信)
- Linux下线程创建进程的方法研究(linux线程创建进程)
- Linux进程和线程:深入了解如何实现并行处理(linux的进程与线程)
- Linux中管理进程的实战技巧(linux进程2)
- Linux下多进程多线程实现实时运算(linux进程线程)
- Linux系统中实现进程间通信的方式(linux系统进程间通信)
- 利用Linux ps命令轻松管理进程,提高系统性能(linuxps进程)
- 深入解析Linux进程PID管理机制(linux进程pid)
- Linux动态进程:提升应用程序效能(linux动态进程)
- 本 Redis老版本从历史角度看进步的进程(redis老版)
- 深入探讨linux下进程的最大线程数、进程最大数、进程打开的文件数
- 输出java进程的jstack信息示例分享通过线程堆栈信息分析java线程