这是我的程序:
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
既然是多进程,为什么process2就一定要竞争的过process3?
你这个例子中是对stdout的竞争。