【python并发】[subprocess库] 优雅的并发模板:并发,多进程管理与交互

需求

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=[]
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoesM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值