2013-03-21 74 views
0

我的代码如下。调用concurrent.futures并行作业的麻烦

executor = concurrent.futures.ThreadPoolExecutor(max_workers=4) 

for cj in self.parent_job.child_jobs: 
    executor.map(cj.runCommand()) 

高清runCommand(个体经营): 使用os.system(self.cmd_line) verifyOutputFiles() ...

runCommand需求并行所有child_jobs执行。一次只能将一个child_job传递给runCommand。

但runCommand一次只能调用一次。但是我需要在同一时间为所有儿童工作调用它。任何有助于实现这一目标,在executor.map API认识

回答

1

看:http://docs.python.org/dev/library/concurrent.futures.html#concurrent.futures.Executor.map 您可以通过调用函数并传递它的结果map犯的错误;)这就是为什么你的代码运行一次。

您需要创建一个单独的功能,这将在对象的方法runCommand被称为你不能通过lambda x: x.runCommand()(一般拉姆达)作为参数传递给executor.map

def runCommand(cj): 
    return cj.runCommand() 

l = executor.map(runCommand, self.parent_job.child_jobs) 

等到所有任务compleate你必须评估发电机l。所以你可以做w = list(l)w将包含结果

+0

child_jobs有要执行的命令列表。我需要并行运行它们。 – Hema 2013-03-21 13:47:25

+0

cj只有一个子作业,self.parent_job.child_jobs有所有子作业的列表。如果我们像executor.map(cj.runCommand,self.parent_job.child_jobs)一样执行,哪一个会到达runCommand – Hema 2013-03-21 13:56:42

+0

我不确定@AndrewDalke建议的是什么。我认为'cj.runCommand'绑定到'cj'实例,所以对我来说没有意义。我认为我发布的内容应该有效 - @ user2194611你试过了吗? – kkonrad 2013-03-21 21:51:47