python3多进程执行循环_python多进程程序的执行次序

这是我的程序:

import os,time

from multiprocessing import Process

def run_proc(name):

print('child process %s (%s) running ...' %(name,os.getpid()))

print('process will start'+name)

time.sleep(5)

print('end '+name)

if __name__ == '__main__':

print('parent process %s.' %os.getppid())

for i in range(5):

p = Process(target=run_proc,args=(str(i),))

p.start()

time python3 test1.py

parent process 4049.

child process 0 (7054) running ...

process will start0

child process 1 (7055) running ...

process will start1

child process 3 (7057) running ...

process will start3

child process 2 (7056) running ...

process will start2

child process 4 (7058) running ...

process will start4

end 0

end 1

end 3

end 2

end 4

real 0m5.099s

user 0m0.080s

sys 0m0.008s

为何执行结果不是

time python3 test1.py

parent process 4049.

child process 0 (7054) running ...

process will start0

child process 1 (7055) running ...

process will start1

child process 2 (7057) running ...

process will start3

child process 3 (7056) running ...

process will start2

child process 4 (7058) running ...

process will start4

end 0

end 1

end 2

end 3

end 4

real 0m5.099s

user 0m0.080s

sys 0m0.008s

既然是多进程,为什么process2就一定要竞争的过process3?

你这个例子中是对stdout的竞争。

因为:process2的产生在时间上,早于process3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值