这是代码:
from time import sleep
from random import random
from multiprocessing import Process
def f():
for i in range(5):
print("hola" , i)
sleep(random())
if __name__ == "__main__":
p = Process(target=f)
q = Process(target=f)
p.start()
q.start()
print("fin")
## sleep(1000)这是我一直得到的输出:
fin
('hola', 0)
('hola', 1)
('hola', 2)
('hola', 3)
('hola', 4)
('hola', 0)
('hola', 1)
('hola', 2)
('hola', 3)
('hola', 4)但是代码不包含任何东西来阻止这两个进程混合,那么为什么它们不混合呢?
Windows 8,Python 2.7,使用anaconda中spyder的最后版本
发布于 2016-03-07 15:24:32
我将冒险说这是Windows:只需在上运行Python2.7.10的示例,我就会得到一个更预期的结果:
In [27]: def f():
....: for i in range(5):
....: print("hola" , i)
....: sleep(random())
....:
In [28]: if __name__ == "__main__":
....: p = Process(target=f)
....: q = Process(target=f)
....: p.start()
....: q.start()
....: print("fin")
....:
fin
In [29]: ('hola', 0)
('hola', 0)
('hola', 1)
('hola', 2)
('hola', 1)
('hola', 3)
('hola', 4)
('hola', 2)
('hola', 3)
('hola', 4)看看这是否有用:https://docs.python.org/2/library/multiprocessing.html#windows
https://stackoverflow.com/questions/35847207
复制相似问题