19091802使用subprocess

本文深入探讨了Python中使用subprocess模块管理子进程的方法,包括call、check_call、check_output及Popen函数的详细应用。通过实例展示了如何处理子进程的输出、错误以及超时情况,特别关注了Popen在复杂场景下的阻塞问题解决方案。

 

目录

 

subprocess.call()

subprocess.check_out()

subprocess.check_output()

subprocess.Popen()


subprocess.call()

res = subprocess.call('ls -l', shell = True)
print  'res:', res

subprocess.check_out()

import subprocess
try:
    res = subprocess.check_call(['ls', '('])
    print  'res:', res
except subprocess.CalledProcessError, exc:
    print 'returncode:', exc.returncode
    print 'cmd:', exc.cmd
    print 'output:', exc.output

subprocess.check_output()

import subprocess
try:
    res = subprocess.check_output('ls xxx',
                    stderr = subprocess.STDOUT,
                    shell = True)
    print  'res:', res
except subprocess.CalledProcessError, exc:
    print 'returncode:', exc.returncode
    print 'cmd:', exc.cmd
    print 'output:', exc.output

subprocess.Popen()

阻塞问题:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
from threading import Timer
import os

class test(object):
    def __init__(self):
        self.stdout = []
        self.stderr = []
        self.timeout = 10
        self.is_timeout = False
        pass
    
    def timeout_callback(self, p):
        self.is_timeout = True
        print 'exe time out call back'
        print p.pid
        try:
            os.killpg(p.pid, signal.SIGKILL)
        except Exception as error:
            print error

    def run(self):
        cmd = ['bash', '/home/zhangxin/work/baofabu/test.sh']
        #p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
        my_timer = Timer(self.timeout, self.timeout_callback, [p])
        my_timer.start()
        try:
            print "start to count timeout; timeout set to be %d \n" % (self.timeout,)
            for line in iter(p.stdout.readline, b''):
                print line
                if self.is_timeout:
                    break
            for line in iter(p.stderr.readline, b''):
                print line
                if self.is_timeout:
                    break
        finally:
            my_timer.cancel()
            p.stdout.close()
            p.stderr.close()
            p.kill()
            p.wait()

其中test.sh:

#!/bin/bash
ping   www.baidu.com
echo $$

像threading那样执行python函数:

import os
import signal
import subprocess
import tempfile
import time
import sys


def show_setting_prgrp():
    print('Calling os.setpgrp() from {}'.format(os.getpid()))
    os.setpgrp()
    print('Process group is now {}'.format(
        os.getpid(), os.getpgrp()))
    sys.stdout.flush()

# 这次的重点关注是这里
script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python signal_child.py
'''
script_file = tempfile.NamedTemporaryFile('wt')
script_file.write(script)
script_file.flush()

proc = subprocess.Popen(
    ['sh', script_file.name],
    preexec_fn=show_setting_prgrp,
)
print('PARENT      : Pausing before signaling {}...'.format(
    proc.pid))
sys.stdout.flush()
time.sleep(1)
print('PARENT      : Signaling process group {}'.format(
    proc.pid))
sys.stdout.flush()
os.killpg(proc.pid, signal.SIGUSR1)
time.sleep(3)

 

Python 的 subprocess 模块用于创建和管理子进程,执行外部命令或脚本,提供了灵活的方法来启动进程、与进程交互、捕获输出,并处理错误[^1]。以下是该模块的核心方法、使用方式及示例: ### subprocess.run() `subprocess.run()` 可用于执行简单命令,它会等待命令执行完成并返回一个 `CompletedProcess` 对象。 ```python import subprocess # 执行简单的系统命令,如列出当前目录下的文件和文件夹 result = subprocess.run(['ls', '-l'], capture_output=True, text=True) print("返回码:", result.returncode) print("标准输出:", result.stdout) print("错误输出:", result.stderr) ``` 在上述代码中,`capture_output=True` 表示捕获命令的标准输出和错误输出,`text=True` 表示以文本形式返回输出内容。 ### subprocess.Popen() `subprocess.Popen()` 提供更灵活的控制,它不会等待命令执行完成,而是返回一个 `Popen` 对象,可以对该对象进行更多操作。 ```python import subprocess # 执行命令 process = subprocess.Popen(['ping', 'www.python.org'], stdout=subprocess.PIPE, text=True) # 读取命令输出 while True: output = process.stdout.readline() if output == '' and process.poll() is not None: break if output: print(output.strip()) # 获取返回码 returncode = process.poll() print("返回码:", returncode) ``` 在上述代码中,`stdout=subprocess.PIPE` 表示将命令的标准输出重定向到管道,通过 `process.stdout.readline()` 逐行读取输出内容。 ### 注意事项 使用 `shell=True` 时需谨慎,避免 shell 注入风险。因为 `shell=True` 会启动一个新的 shell 来执行命令,可能会导致安全问题。例如: ```python import subprocess # 不推荐的使用方式 user_input = '; rm -rf /' # 模拟恶意输入 command = f'echo {user_input}' subprocess.run(command, shell=True) # 存在安全风险 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值