在p中调用并发任务出现问题

2024-09-20 22:53:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码如下。你知道吗

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

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

def runCommand(自身): 操作系统(self.cmd\u行) 验证输出文件() ... 你知道吗

runCommand需要为所有子作业并行执行。同时,一次只能将一个子作业传递给runCommand。你知道吗

但是runCommand一次只能调用一次。但我需要同时调用所有子作业。任何帮助实现这一点都是非常感谢的


Tags: 代码inselffor作业jobconcurrentmax
1条回答
网友
1楼 · 发布于 2024-09-20 22:53:21

看看executor.mapAPI:http://docs.python.org/dev/library/concurrent.futures.html#concurrent.futures.Executor.map 调用函数并将其结果传递给map;,这就是代码只运行一次的原因。你知道吗

您需要创建单独的函数,该函数将在方法runCommand的对象上调用,因为您不能将lambda x: x.runCommand()(通常是lambda)作为参数传递给executor.map。你知道吗

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

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

要等到所有任务完成,您必须计算生成器l。所以你可以做w = list(l)w将包含结果

相关问题 更多 >

    热门问题