前端之家收集整理的这篇文章主要介绍了
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.id
7992
child p.name:Process
-2
p.id
4204
child p.name:Process
-1
p.id
6380
END!!!!!!!!!!!!!!!!!
worker_
1
worker_
3
worker_
2
end worker_
1
end worker_
2
end worker_
3