我想确保以下两个并行进程一个接一个地执行。特别是,我希望首先实现十个f函数,并在完成该部分之后,实现十个g函数。有谁知道我应该如何修改我的代码?如何随后运行两个并行进程?
from multiprocessing import Process
import time
import random
wait_low = 0.1
wait_high = 15
def f(i):
time.sleep(random.uniform(wait_low, wait_high))
print 'hello'+str(i)
def g(i):
time.sleep(random.uniform(wait_low, wait_high))
print 'hey'+str(i)
if __name__ == '__main__':
for j in range(10):
p = Process(target=f, args=(j,))
p.start()
p.join()
print "switch"
# comment
for j in range(10):
q = Process(target=g, args=(j,))
q.start()
q.join()
time.sleep(5)
print "I'm done"
而且我得到的结果是:
hello2
hello0
hello1
hello5
hello6
hello8
hello3
hello7
hello9
switch
hey6
hey3
hello4
hey9
hey8
I'm done
hey2
hey0
hey1
hey5
hey7
hey4
非常感谢!
这非常非常感谢 – user3698176
@ user3698176,没问题,请接受的解决方案。!。 – Fabricator