Python 创建函数并将其作为多个进程的完整代码

前端之家收集整理的这篇文章主要介绍了Python 创建函数并将其作为多个进程的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣Python 创建函数并将其作为多个进程的完整代码的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。<br>
#  编程之家  (jb51.cc) 
import multiprocessing
import time

def worker_1(interval):
    print "worker_1"
    time.sleep(interval)
    print "end worker_1"

def worker_2(interval):
    print "worker_2"
    time.sleep(interval)
    print "end worker_2"

def worker_3(interval):
    print "worker_3"
    time.sleep(interval)
    print "end worker_3"

if __name__ == "__main__":
    p1 = multiprocessing.Process(target = worker_1,args = (2,))
    p2 = multiprocessing.Process(target = worker_2,args = (3,))
    p3 = multiprocessing.Process(target = worker_3,args = (4,))

    p1.start()
    p2.start()
    p3.start()

    print("The number of cpu is:" + str(multiprocessing.cpu_count()))
    for p in multiprocessing.active_children():
        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))
    print "END!!!!!!!!!!!!!!!!!"
结果:
The number of cpu is:4
child   p.name:Process-3    p.id7992
child   p.name:Process-2    p.id4204
child   p.name:Process-1    p.id6380
END!!!!!!!!!!!!!!!!!
worker_1
worker_3
worker_2
end worker_1
end worker_2
end worker_3
原文链接:https://www.f2er.com/python/526817.html

猜你在找的Python相关文章