#!/usr/bin/env python3importargparseimportmultiprocessingimportsubprocess#Key is a 'friendly' script name, value is a tuple of command to run and argumentsSCRIPT_NAMES={'test1':('./test1.sh'),'test2':('./test2.sh'),'test3':('./test3.sh')}defmain():parser=argparse.ArgumentParser()#num_processes is the number of processes to split the scripts over, defaults to cpu count (1 to run serially)parser.add_argument('-n','--num_processes',type=int,help='number of processes to split scripts across, default={0}'.format(multiprocessing.cpu_count()),default=multiprocessing.cpu_count())#scripts is the list of scripts to run, run all scripts by defaultparser.add_argument('scripts',type=str,nargs='*',help='the scripts to run, scripts available: {0}'.format(', '.join([*SCRIPT_NAMES])),default=[*SCRIPT_NAMES])args=parser.parse_args()#Build a multiprocessing pool with the requested process countpool=multiprocessing.Pool(args.num_processes)#Run in parallel, aggregating resultsresults=pool.map(run_func,args.scripts)#Print the results, line by lineforresultinresults:print(result)defrun_func(to_run):#Run the requested script, capturing standard outprocess=subprocess.Popen(SCRIPT_NAMES[to_run],stdout=subprocess.PIPE)#Bang the results into a stringreturnprocess.communicate()[0].decode('utf-8')if__name__=='__main__':main()