例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起进程函数的信息别忘了在本站进行查找喔 。
推荐阅读
- 王者云果果在哪里直播卖货,王者果果
- java自动清屏代码,java 清空屏幕
- word如何导出语音,导出word朗读音频
- 单机游戏恋爱养成推荐,单机恋爱养成类手游
- mysql怎么输出数据库 mysql输出数据库中所有表
- go语言计算题,C语言计算题
- 鸿蒙3如何更换主题,鸿蒙主题设置
- 手机怎么看qq空间私密日志,空间里的私密日志怎样用手机看
- 番茄计时java代码 番茄计时app