我是否正确使用了 python 的 apply_async?
Am I using python's apply_async correctly?
这是我第一次尝试在 Python 中使用多处理。我正在尝试按行并行化我的函数 fun
在我的数据帧 df
上。回调函数只是将结果附加到一个空列表,稍后我将对它进行排序。
这是apply_async
的正确使用方法吗?非常感谢。
import multiprocessing as mp
function_results = []
async_results = []
p = mp.Pool() # by default should use number of processors
for row in df.iterrows():
r = p.apply_async(fun, (row,), callback=function_results.extend)
async_results.append(r)
for r in async_results:
r.wait()
p.close()
p.join()
看起来使用 map or imap_unordered(取决于您是否需要对结果进行排序)更符合您的需求
import multiprocessing as mp
#prepare stuff
if __name__=="__main__":
p = mp.Pool()
function_results = list(p.imap_unorderd(fun,df.iterrows())) #unordered
#function_results = p.map(fun,df.iterrows()) #ordered
p.close()
这是我第一次尝试在 Python 中使用多处理。我正在尝试按行并行化我的函数 fun
在我的数据帧 df
上。回调函数只是将结果附加到一个空列表,稍后我将对它进行排序。
这是apply_async
的正确使用方法吗?非常感谢。
import multiprocessing as mp
function_results = []
async_results = []
p = mp.Pool() # by default should use number of processors
for row in df.iterrows():
r = p.apply_async(fun, (row,), callback=function_results.extend)
async_results.append(r)
for r in async_results:
r.wait()
p.close()
p.join()
看起来使用 map or imap_unordered(取决于您是否需要对结果进行排序)更符合您的需求
import multiprocessing as mp
#prepare stuff
if __name__=="__main__":
p = mp.Pool()
function_results = list(p.imap_unorderd(fun,df.iterrows())) #unordered
#function_results = p.map(fun,df.iterrows()) #ordered
p.close()