1 作用

为了连贯性和方便操作,经常需要在python代码种加入shell命令。

2 方案

2.0 jupyter中使用

使用前面加 !号的魔法函数,如下所示,直接执行:
image.png
这种好处是直接输出信息,和2.1方案一不同,下面的方案一不利于排错。

2.1.方案一

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

  1. import os
  2. os.system('ls -l')

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

  • 在jupyter中执行,不报错,会输出非0数字
  • 注意路径的问题,因为运行路径和要处理的路径有不一致的。例如:之前没注意到路径问题,导致差错困难,最后还是通过2.0方式看到的错误,为文件夹不存在。

image.png

2.2.方案二

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

  1. def __exec_command(cmd: str, input: str = None, timeout=10) -> str:
  2. try:
  3. output_bytes = subprocess.check_output(cmd, input=input, stderr=subprocess.STDOUT, shell=True, timeout=timeout)
  4. except subprocess.CalledProcessError as err:
  5. output = err.output.decode('utf-8')
  6. logger.debug(output)
  7. raise err
  8. result = output_bytes.decode('utf-8')
  9. return result
  10. print(__exec_command('ls -l'))

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

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

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

2.3.方案三

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

  1. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
  2. while True:
  3. if process.poll() is None and timeout > 0:
  4. output_bs = process.stdout.read()
  5. if not output_bs:
  6. ....
  7. time.sleep(0.5)
  8. timeout = timeout - 0.5
  9. if process.poll() is None or timeout <= 0:
  10. process.kill()

上述问题是无法解决我们的问题,因为process.stdout.read()是阻塞的,如果子进程没有输出,就挂住了。
我们使用有超时功能communicate方法再试试:

  1. def exec_command(cmd: str, input: str = None, encoding='utf-8', shell=True, timeout=5) -> str:
  2. output_bytes = b''
  3. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
  4. while process.poll() is None and timeout > 0:
  5. try:
  6. output_bytes, output_err_bytes = process.communicate(timeout=0.5)
  7. except subprocess.TimeoutExpired as err:
  8. if err.stdout:
  9. output = err.output.decode(encoding)
  10. print(output)
  11. timeout -= 0.5
  12. continue
  13. if process.poll() is None or timeout <= 0:
  14. process.kill()
  15. raise ValueError(f'exec command: {cmd} timeout')
  16. result = output_bytes.decode(encoding)
  17. return result

communicate超时时抛出异常subprocess.TimeoutExpired,这个异常对象的stdout带有子进程已经输出的内容。
目前还不错,可以满足开头提的问题。不过,不能算完美,因为输出是有点奇怪,如下所示:

  1. hello
  2. hello
  3. hello
  4. hello
  5. hello
  6. hello
  7. hello-end

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

2.4.方案四

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

  1. def exec_command(cmd: str, input: str = None, encoding='utf-8', shell=True, timeout=10, wait=0.5) -> str:
  2. _opener = lambda name, flag, mode=0o7770: os.open(name, flag | os.O_RDWR, mode)
  3. output_bytes = bytearray()
  4. with tempfile.NamedTemporaryFile('w+b') as writer, open(writer.name, 'rb', opener=_opener) as reader:
  5. try:
  6. process = subprocess.Popen(cmd, stdout=writer, stderr=writer, stdin=subprocess.PIPE, shell=shell)
  7. if input:
  8. process.stdin.write(input.encode(encoding))
  9. process.stdin.close()
  10. while process.poll() is None and timeout > 0:
  11. new_bytes = reader.read()
  12. if new_bytes or new_bytes != b'':
  13. logger.debug(f'{new_bytes}')
  14. output_bytes = output_bytes + bytearray(new_bytes)
  15. else:
  16. logger.debug('waiting sub process output......')
  17. time.sleep(wait)
  18. timeout -= wait
  19. except Exception as err:
  20. process.kill()
  21. raise err
  22. if process.poll() is None:
  23. process.kill()
  24. raise ValueError(f'exec cmd:{cmd} timeout')
  25. new_bytes = reader.read()
  26. if new_bytes:
  27. output_bytes = output_bytes + bytearray(new_bytes)
  28. result = output_bytes.decode(encoding)
  29. return result

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

3.讨论

在windows系统中,python创建子进程的时候,可以使用管道作为子进程的输入参数startupinfo,从而完美实现子进程输入输出重定向。但在linux确不行,不支持参数startupinfo。
process=subprocess.Popen参数subprocess.PIPE字面上是返回管道,但子进程process.stdout实际是文件句柄,读操作完全是阻塞,没有非阻塞得读,这是问题的关键所在。
方案二和方案四不妨结合起来使用,对于长时间执行任务,选择方案四,对于一般的任务执行直接使用方案二。
python中执行系统shell命令,也可以创建一个线程进行子进程输出读取,超时就杀掉线程;或者使用协程版本的subprocess,但是实现起来更加复杂,效率显得更差。有兴趣的同学,可以自己实现试试。
基本来自:原文链接:https://blog.csdn.net/KiteRunner/article/details/119855323