0
假设我有这两个来完成相同的任务的方法:python multiprocessing starmap vs apply_async,哪个更快?
from multiprocessing import Pool
pool = Pool(4)
def func(*args):
# do some slow operations
return something
dates = ['2011-01-01', ' 2011-01-02', ... , '2017-01-01']
other_args = [1, 2, 3, 'c', 'test', 'pdf')]
# approach 1:
res = [pool.apply_async(func, [day] + other_args) for day in dates]
list_of_results = [x.get() for x in res]
# approach 2: create an iterable of iterables
args = [[day] + other_args for day in dates]
list_of_results = pool.starmap(func, args)
我马上意识到apply_async回报,然而,x.get()仍然可能阻塞主线程,如果FUNC尚未运行完毕......威尔这两种方法之间必然存在性能差异?
使用异步方法的关键在于避免等待结果,因为它们将在以后使用。 –