我想確保以下兩個並行進程一個接一個地執行。特別是,我希望首先實現十個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