[英]Python : Parallel execution of function
我想并行执行一组任务。 我在一个类中定义了一个函数,它接受参数并根据参数执行操作。 类结构如下。
from threading import Thread
from concurrent.futures import *
class Test(object):
def process_dataframe(self,id:int):
print(id*id)
def run_task(self):
thd = []
for i in range(1,10):
thd.append( "self.process_dataframe({0})".format(i))
self.run_functions_in_parallel(thd)
def run_functions_in_parallel(self,fns)->bool:
def wrap_function(self,fnToCall):
try:
eval(fnToCall)
return ("0")
except Exception as e:
return "{0}".format(e)
thd = []
isError = False
executor = ThreadPoolExecutor(max_workers=len(fns))
errorMessage = ""
for fn in fns:
t = executor.submit(wrap_function,self,fn)
thd.append(t)
for td in thd:
ret = td.result()
if ret != "0":
isError = True
errorMessage = errorMessage + """
""" + ret
if isError == True:
print (errorMessage)
raise Exception (errorMessage)
else:
return True
d=Test()
d.run_task()
我已经设法让它工作并且任务正在正确执行。 我想知道是否有更好/更简单的方法来完成同样的任务。 我想保持 run_functions_in_parallel 方法通用,以便它可以用作模块中的通用方法。
您不需要使用包装器,因为ThreadPoolExecutor
以更好的方式捕获错误。 一个总是返回True
或引发错误的函数不需要返回值,但如果你有返回值的函数,你想并行调用,你应该返回它们的结果。 使用魔术字符串作为错误指示符是一个坏主意。 KeyError: 0
format(e)
也会导致"0"
。 最好使用唯一值,例如在我们的示例中为None
。 如果不需要,请不要使用eval
。 在您的情况下,您可以使用partial
。 不要对max_workers
使用太大的值。
from functools import partial
from concurrent.futures import ThreadPoolExecutor
class Test(object):
def process_dataframe(self, id):
print(id*id)
def run_task(self):
functions = []
for i in range(1,10):
functions.append(partial(self.process_dataframe, i))
self.run_functions_in_parallel(functions)
def run_functions_in_parallel(self, functions, max_workers=8):
executor = ThreadPoolExecutor(max_workers=max_workers)
futures = [
executor.submit(function)
for function in functions
]
errors = []
results = []
for future in futures:
try:
result = future.result()
except Exception as e:
errors.append(e)
else:
results.append(result)
if errors:
raise Exception(errors)
return results
d = Test()
d.run_task()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.