【发布时间】:2019-12-02 00:02:13
【问题描述】:
我有以下函数来并行化一些任务:
def parallelize(func, variable_args, proc_count=None):
if proc_count is None:
proc_count = multi.cpu_count()
warnings.warn('You have {n_cpu} CPU. Tasks will be distributed over all.'.format(n_cpu=proc_count))
pool = multi.Pool(processes=proc_count)
result = pool.map(func, variable_args)
pool.close()
pool.join()
return result
我有一堆看起来像这样的旧函数:
def some_old_function(that_,takes_,lots_of_,arguments_):
do_stuff_single_thread()
到目前为止,我为“并行化”这些函数所做的工作是将它们重新编码为如下所示:
def some_old_function(dict_arg):
that_ = dict_arg['that']
takes_= dict_arg['takes_']
...
do_stuff_single_thread()
最后调用:
parallelize(some_old_function, list_of_dict_args)
我之所以写,是因为我担心必须有一种比我上面选择的方式更“pythonic”的方式来做到这一点。
感谢我收到的任何建议。
【问题讨论】:
标签: python python-2.7 multiprocessing