I am using python3 subprocess module to cope with bash script call. I want to control the number of concurrent subprocess when calling the script.
Popen.wait(timeout=None) Wait for child process to terminate. Set and return returncode attribute.
I know that I can call the subprocess.Popen.wait(timeout=None) to wait for child process to terminate. But I was wondering about if I can wait for a list of subprocess.Popen to finish, with that, I can control the number of concurrent processes.
Sample code snippets as following:
Example.py
import subprocess
from itertools import zip_longest
seed = [1, 2, 4, 1, 3, 5, 4, 1]
basepath="/path/to/file/hello.sh"
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def bash_handler(num):
return subprocess.Popen('bash {0} {1}'.format(basepath, num), shell=True)
for bulk in grouper(seed, 2):
[bash_handler(item).wait() for item in bulk if item is not None]
# This will executed one by one
hello.sh
# Validation goes here.
echo $1
sleep $1
echo "Finish Executed"