在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  網絡安全/ 請問:Python的multi-processing模塊怎么讓快的任務等待慢的任

請問:Python的multi-processing模塊怎么讓快的任務等待慢的任務?

請教一個Python的multi-processing模塊互相等待、防止競爭的問題?,F(xiàn)在我有2個任務,任務1和任務2,要么1比2快、要么2比1快?,F(xiàn)在要并發(fā)同時執(zhí)行2個任務,快的完畢之后等待慢的。等待2個任務都結束之后,然后系統(tǒng)再轉而運行第3個任務。

我嘗試了一下apply_async:

import time
from multiprocessing import Pool

def func1():
    print "This starts task1."
    time.sleep(1)
    print "This ends task1."
def func2():
    print "This starts task2."
    time.sleep(2)
    print "This ends task2."
def func3(x):
    print x
    
pool = Pool(processes=3)
result = pool.apply_async(func1, func2)
pool.start()
pool.join()

我覺得還是不對,但是不清楚正確的應該怎么寫。另外,平臺是Python2.7,所以高級一點的Asyncio就沒法用了。請問這個快的等待慢的、然后兩個全部結束后再運行新任務的并發(fā)執(zhí)行應該怎么設計呢?

謝謝了先!

回答
編輯回答
久不遇

apply_async(func[, args[, kwds[, callback]]])
你的用法明顯不對嗎。

import time
from multiprocessing import Pool

def func(n):
    print "This starts task{}.".format(n)
    time.sleep(n)
    print "This ends task{}.".format(n)

    
pool = Pool(processes=3)
pool.map(func, range(3))
pool.join()
2018年8月7日 16:15