2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > python 多进程multiprocessing 模块

python 多进程multiprocessing 模块

时间:2020-08-24 05:06:51

相关推荐

python 多进程multiprocessing 模块

multiprocessing 常用方法:

cpu_count():统计cpu核数

multiprocessing.cpu_count()

active_children() 获取所有子进程

multiprocessing.active_children()

preces() 创建一个进程对象

multiprocessing.Preces(target=function_name, args=())

target: 函数名args: 函数需要的参数,以tuple形式传入,一个参数时需(1,)

Preces 常用方法:

is_alive() 判断进程是否存在

run() 启动进程

start() 启动进程,会自动调用run方法,这个常用

join([timeout]) 等待进程结束或者直到超时 join() 方法说明:

def def worker(interval):time.sleep(interval)print('hello world')P = multiprocessing.Process(target=worker, args=(5,))#-----------------------------------P.start()#设置timeout 设置超时时间print(P.is_alive())P.join(timeout=3)print('end main')###Trueend mainhello world#-----------------------------------P.start()P.alive()# 不调置timeout 超时时间P.join()print()###Truehello worldend main#-----------------------------------结论:当join()不设置timeout时程序会一直等待上面的进程执行完成后再执行join()后面的代码当设置timeout时,无论上面的进程是否执行完成,程序运行到指定时间后就会执行后面的代码

Preces 常用属性

namd 进程名子

pid 进程的pid

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。