如何并行运行子流程,等待它们完成,然后运行子流程

2024-09-27 23:18:04 发布

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

我尝试并行运行GDAL(图像处理),然后将输出文件的路径存储在一个列表中,该列表应该写入一个文件。此文件需要在单个子进程命令中使用。你知道吗

我对多进程/子进程没有太多经验,但我读过 Run multiple subprocesses in parallel - python 2.7

How do I run multiple subprocesses in parallel and wait for them to finish in Python

第二个问题是我目前的大部分代码都基于什么。但是,它返回一些错误:

此代码段并行运行:

    childprocesses = []

    for fil in files: #files is list of input .tif files
        p = Popen(['gdal_translate', 'cmd2', 'cmd3' #different settings for gdal
        , fil 
        , os.path.join(cfolder,os.path.basename(fil))] #output
        , stdout=PIPE
        , stderr=PIPE)
        childprocesses.append(p)
    for cp in childprocesses:
        cp.wait
    print ('translate complete')

之后,我尝试将输出路径存储在一个列表(cfiles)中,并将其写入一个文件(temp_file)。此文件和文件列表随后在另一个gdal命令中使用:

cfiles = glob.glob('{cfol}{sep}*.tif'.format(cfol=cfolder, sep=os.sep))
print (cfiles)
with open(temp_file, 'w') as f:
    for line in cfiles:
         f.write(line+'\n')
subprocess.check_output(['gdalbuildvrt',
 '-hidenodata', '-input_file_list', tempfile, outputdir])

但是,当我运行整个代码时,它会将行“translate complete”打印9次(我的testinput中有9个tif文件)和一个包含数据集的列表。但是,子进程找不到文件:

translate complete
translate complete
translate complete
translate complete
translate complete
translate complete
translate complete
translate complete
translate complete
[]

FAILURE: No input filenames specified.  

子进程gdalbuildvrt返回失败,因为文本文件为空。我不知道为什么会这样。我假设这是因为gdal_translate在全球。全球文件列表已运行。你知道吗

我所希望的是,它将等待所有gdal_translate子进程完成,然后列出文件,并运行gdalbuildvrt。你知道吗


Tags: 文件in列表forinput进程osfiles

热门问题