进程、线程、协程开启方式

复制代码
__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)
复制代码

 

posted @   一石数字欠我15w!!!  阅读(519)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示