-
nigger说:
python subprocess限制子进程数量代码
import subprocess from multiprocessing import Pool def run_cmd(cmd): ''' 执行shell 命令 ''' return subprocess.call(cmd, shell=True) if __name__ == '__main__': # 定义需要执行的命令 cmds = [ "ls -l", "df -h", "free -m" ] # 创建进程池,最大进程数为3 pool = Pool(3) # 执行命令 pool.map(run_cmd, cmds) # 关闭进程池 pool.close()