linux的多进程工具parallel不好用
编写了一个脚本,为了搭配编程语言,方便使用多进程。对手工使用不太友好
参数
参数名 | 含义 |
无参数名 | 要并行运行的命令,用单/双引号包围,空格分隔 |
-p | 在每个并行命令运行前执行的命令 |
-h | 显示帮助信息并退出 |
用法
见下方示例
脚本
#!/usr/bin/env python3 import sys import subprocess as sp args = sys.argv[1:] if '-h' in args or '--help' in args: print('help message:\n\tcommands\tmulti-processing commands\n\t-p\tprecommands\n\t-h/--help\tshow help message') exit(0) pres = [] cmds = [] it = iter(args) for i in it: if i == '-p': pres.append(next(it)) else: cmds.append(i) pre = '&&'.join(pres) + '&&' if pres else ''
ps = [] for cmd in cmds: ps.append(sp.Popen(pre+cmd, shell=True)) for p in ps: p.wait()
示例
脚本名:start_pses_and_wait
在创建脚本并保存后,在linux命令行下输入:
python3 start_pses_and_wait "sleep 10" "echo 3 > a.txt" "touch b.txt" "sleep 5"
可以发现程序运行了10秒(说明休息10秒和休息5秒的命令在同时执行),多出了两个文件a.txt b.txt。
标签:help,编程语言,自编,args,append,Linux,txt,pres,wait From: https://www.cnblogs.com/roundfish/p/17392974.html