运维辞典@欢迎访问小站,如有疑问,可通过右侧QQ或底部邮箱联系,非常感谢@

python多进程示例代码

Python Administrator 421℃
import time
import multiprocessing

# cpu线程数量,一般和cpu核心数一样
max_process = 4

# cpu密集型
def fun(n, n2):
    for i in range(0, n):
        for j in range(0 , (int)(n*n*n*n2)):
            t = i*j

def process_main(n2):
    p = multiprocessing.Pool(max_process)
    for i in range(0,max_process):
        p.apply_async(func=fun,args=(50,n2))
    print("start time:",time.ctime())
    p.close()
    p.join()
    print("end time:",time.ctime())

if __name__ == '__main__':
    process_main(22)

码字很辛苦,转载请注明来自:《运维辞典 » python多进程示例代码

喜欢 (0)