如何让多处理稍等一下?

时间:2019-07-19 20:45:51

标签: python python-2.7 multiprocessing

现在,我有以下函数test()

def test():
    f1(par)  # extremely time consuming
    f2()  # f2 must be executed after f1()  

f1()在时间上非常昂贵。 f2()必须在f1()之后执行。 我想使用多重处理来加速f1(),所以我修改了test():

import multiprocessing as mp
from multiprocessing import Pool


def test():
    pool = Pool(processes=mp.cpu_count())
    res = [pool.apply_async(f1, (p)) for p in list_pars]
    # 
    # ??? pause or what?
    # 
    f2()  # f2 must be executed after f1()  

f1()加快了速度,但是在f1()完成之前执行了f2()。 如何让程序稍等一下?在完成f1()之后,它将开始执行f2()。我应该如何修改密码?

谢谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用某种线程同步? herehere

我会写pool.join()