python起进程函数 python 进程pid( 四 )


例1.1:创建函数并将其作为单个进程
import multiprocessingimport timedef worker(interval):
n = 5while n0:print("The time is {0}".format(time.ctime()))
time.sleep(interval)
n -= 1if __name__ == "__main__":
p = multiprocessing.Process(target = worker, args = (3,))
p.start()print "p.pid:", p.pidprint "p.name:", p.nameprint "p.is_alive:", p.is_alive()
结果
12345678
p.pid: 8736p.name: Process-1p.is_alive: TrueThe time is Tue Apr 21 20:55:12 2015The time is Tue Apr 21 20:55:15 2015The time is Tue Apr 21 20:55:18 2015The time is Tue Apr 21 20:55:21 2015The time is Tue Apr 21 20:55:24 2015
例1.2:创建函数并将其作为多个进程
import multiprocessingimport timedef 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("childp.name:" + p.name + "\tp.id" + str(p.pid))print "END!!!!!!!!!!!!!!!!!"
结果
1234567891011
The number of CPU is:4childp.name:Process-3p.id7992childp.name:Process-2p.id4204childp.name:Process-1p.id6380END!!!!!!!!!!!!!!!!!worker_1worker_3worker_2end worker_1end worker_2end worker_3
【python起进程函数 python 进程pid】python起进程函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于python 进程pid、python起进程函数的信息别忘了在本站进行查找喔 。

推荐阅读