Python学习:执行系统shell命令

1.问题

python可以作为shell替代,代码比较直观,易于维护。 python支持调用外部shell命令。不过,这个问题没有看上去简单,要完美的解决此问题,比较复杂,就连标准库也不见得处理得很好。

2.方案

2.1.方案一

首先最简单的方法就是调用system方法,直接执行系统shell命令,代码如下:

import os

os.system('ls -l')

system主要问题,就是无法获取shell命令的输出,无法进行输入;也没有超时设置,如果外部命令挂死,会直接导致当前进程挂死。

2.2.方案二

python3subprocess提供了check_output方法,可以直接获取进程的输出,也支持输入,同时关键的是支持超时设置。这就防止了shell命令挂死的问题。

def __exec_command(cmd: str, input: str = None, timeout=10) -> str:
    try:
        output_bytes = subprocess.check_output(cmd, input=input, stderr=subprocess.STDOUT, shell=True, timeout=timeout)
    except subprocess.CalledProcessError as err:
        output = err.output.decode('utf-8')
        logger.debug(output)
        raise err
    result = output_bytes.decode('utf-8')
    return result

print(__exec_command('ls -l')

现在可以成功获取系统命令的结果,并且很好的支持超时功能,防止命令挂死。不过,我们看看下面这个例子:

print(__exec_command('echo begin;sleep 10; echo end; sleep 3'), timeout=30

上述代码中,要想获取shell命令的结果,实际测试的结果,只能等到子进程结束才可以获取,父进程只能傻傻得等,对子进程的执行过程一无所知。

2.3.方案三

上述的问题,看上容易解决,实际上比较复杂。我们先看下,使用更低层的subprocess.Popen能否解决:

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )

while True:
    if process.poll() is None and timeout > 0:
        output_bs = process.stdout.read()
        if not output_bs:
              ....
        time.sleep(0.5)
        timeout = timeout - 0.5

if process.poll() is None or timeout <= 0:
      process.kill()

上述问题是无法解决我们的问题,因为process.stdout.read()是阻塞的,如果子进程没有输出,就挂住了。

我们使用有超时功能communicate方法再试试:

def exec_command(cmd: str, input: str = None, encoding='utf-8', shell=True, timeout=5) -> str:
    output_bytes = b''
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
    while process.poll() is None and timeout > 0:
        try:
            output_bytes, output_err_bytes = process.communicate(timeout=0.5)
        except subprocess.TimeoutExpired as err:
            if err.stdout:
                output = err.output.decode(encoding)
                print(output)
            timeout -= 0.5
            continue
    if process.poll() is None or timeout <= 0:
        process.kill()
        raise ValueError(f'exec command: {cmd} timeout')

    result = output_bytes.decode(encoding)
    return result

communicate超时时抛出异常subprocess.TimeoutExpired,这个异常对象的stdout带有子进程已经输出的内容。

目前还不错,可以满足开头提的问题。不过,不能算完美,因为输出是有点奇怪,如下所示:

hello
hello
hello
hello
hello
hello
hello-end

每次TimeoutExpired超时,stdout所带的内容,是子进程已经输出的内容,而不是新增加的内容。

2.4.方案四

要想实时获取子进程是否有内容输出,我们可以使用文件进行重定下,代码如下:

def exec_command(cmd: str, input: str = None, encoding='utf-8', shell=True, timeout=10, wait=0.5) -> str:
    _opener = lambda name, flag, mode=0o7770: os.open(name, flag | os.O_RDWR, mode)
    output_bytes = bytearray()
    with tempfile.NamedTemporaryFile('w+b') as writer, open(writer.name, 'rb', opener=_opener) as reader:
        try:
            process = subprocess.Popen(cmd, stdout=writer, stderr=writer, stdin=subprocess.PIPE, shell=shell)
            if input:
                process.stdin.write(input.encode(encoding))
                process.stdin.close()
            while process.poll() is None and timeout > 0:
                new_bytes = reader.read()
                if new_bytes or new_bytes != b'':
                    logger.debug(f'{new_bytes}')
                    output_bytes = output_bytes + bytearray(new_bytes)
                else:
                    logger.debug('waiting sub process output......')
                time.sleep(wait)
                timeout -= wait
        except Exception as err:
            process.kill()
            raise err

        if process.poll() is None:
            process.kill()
            raise ValueError(f'exec cmd:{cmd} timeout')
        new_bytes = reader.read()
        if new_bytes:
            output_bytes = output_bytes + bytearray(new_bytes)

    result = output_bytes.decode(encoding)
    return result

这里,我们试用了临时文件对子进程的输入输出进行重定向,对于文件的读取reader.read()实际上并不是阻塞的。基本完美实现了本文的问题。

3.讨论

windows系统中,python创建子进程的时候,可以使用管道作为子进程的输入参数startupinfo,从而完美实现子进程输入输出重定向。但在linux确不行,不支持参数startupinfo

process=subprocess.Popen参数subprocess.PIPE字面上是返回管道,但子进程process.stdout实际是文件句柄,读操作完全是阻塞,没有非阻塞得读,这是问题的关键所在。

方案二和方案四不妨结合起来使用,对于长时间执行任务,选择方案四,对于一般的任务执行直接使用方案二。

python中执行系统shell命令,也可以创建一个线程进行子进程输出读取,超时就杀掉线程;或者使用协程版本的subprocess,但是实现起来更加复杂,效率显得更差。有兴趣的同学,可以自己实现试试。

enjoy~~~

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值