1> 创建多个进程,并发执行多个终端指令
2> 每个进程的进程号不同(以供记录,并在异常退出时进行进程清理)
3> 每个子进程的输出可被python变量记录 (别问,就是想看)
4> 这些子进程的输出不要显示在终端上 (别问,就是不想看)
进程创建
import subprocess #fork进程
import tempfile #临时文件
child_process = [] #记录所有子进程,以供清理
def sub_boot():
command = 'your shell command'
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
process = subprocess.Popen(command, shell=True,
stdout=temp_file, # write into temp_file
stderr=sys.stdout, # output into you terminal
prexxes_fn=os.setsid) # create new process-id
# do anything you like with process,like record
child_process.append(process)
# because below process would not stop your main process
process,wait() # 阻塞主进程,等待子进程结束
with open(temp_file.name,'r') as file:
txt = file.read()
# after this line , the temp_file would be auto-deleted
print(txt)
# do anything you like with txt
sub_boot()
进程清理
import os
import signal
child_process = []
def sub_kill(process):
main_pid = os.getpid() #获得主进程的进程号
try: #防止进程已经关闭导致的报错
pgid = os.getpgid(process.pid) #获取进程id
if pgid != main_pid: #防止杀死自己
os.killpg(pgid,signal.SIGTERM) #这个信号相当于 ctrl+C
os.killpg(pgid,signal.SIGKILL) # 这个信号会强行杀死
else:
pass
except:
pass
def clean_all():
for process in child_process:
sub_kill(process)
child_process=[]