Hey, pythonians!
Could someone tell me where I’m going wrong, please?
I would like to shutdown a pool of processes when the result of a process returns a specific condition. I’m using the shutdown
method to try to do this, but the method is not working as I expected: the processes are not killed. Should not the method do this?
I’m using the Python 3.9.7.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import glob
from natsort import natsorted
from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor, as_completed
def run(commands):
executor = ProcessPoolExecutor()
futures = []
for cmd in commands:
f = executor.submit(
subprocess.run,
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT
)
futures.append(f)
for f in tqdm(as_completed(futures), total=len(commands)):
if f.result().returncode == 1:
print('subprocess.CompletedProcess.returncode == 1')
executor.shutdown(wait=False, cancel_futures=True)
if __name__ == '__main__':
executable = 'ffmpeg.exe'
filenames = natsorted(glob.glob('*.mp4'))
filenames[3] = 'non-existent-filename.mp4' # add an error
commands = []
for f in filenames:
cmd = [executable, '-y', '-i', f'{f}', f'{f}.mkv']
commands.append(cmd)
run(commands)