Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 4 KB

调用子进程.md

File metadata and controls

105 lines (74 loc) · 4 KB

调用子进程

python经常用于写脚本,写脚本就离不开调用外部程序,这个被称作子进程调用,python原生支持我们可以使用标准库subprocess,如果我们需要异步的使用子进程,还可以使用asyncio.create_subprocess_shell

使用subprocess

我们调用接口subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)来执行子进程的调用,通常我们会比较关注下面几个参数:

  • args要执行的命令行命令,最好使用字符串,也支持字符串组成的序列,
  • capture_output标明是否要捕获子进程的输出
  • timeout表示如果子进程多久没有执行完成会被杀死
  • check表示当子进程报错时这个run函数的执行也会报CalledProcessError
  • shell指定是否使用shell执行程序
  • cwd指定工作的路径,如果是None则在执行脚本所在路径上执行
  • env参数的值只能是字典或者None,用于设定执行时用的环境变量
  • input用于传入执行时的输入,如果有text参数或者使用encoding指定了字符串编码,则传入的是字符串,否则传入的只能是字节串
import subprocess
cp = subprocess.run("ls -la",capture_output=True, shell=True, check=True)
cp.returncode
0
print(cp.stdout.decode("utf-8"))
total 88
drwxr-xr-x  14 huangsizhe  staff    448  6 22 16:12 .
drwxr-xr-x  17 huangsizhe  staff    544  6  6 10:39 ..
-rw-r--r--@  1 huangsizhe  staff   6148  5 15 20:18 .DS_Store
drwxr-xr-x  14 huangsizhe  staff    448  6 21 23:49 .git
-rw-r--r--   1 huangsizhe  staff   1203  4  9 00:57 .gitignore
drwxr-xr-x   5 huangsizhe  staff    160  6 22 15:58 .ipynb_checkpoints
-rw-r--r--   1 huangsizhe  staff   1074  4  9 00:57 LICENSE
-rw-r--r--   1 huangsizhe  staff    494  5 15 18:50 README.md
drwxr-xr-x   2 huangsizhe  staff     64  4  9 01:40 img
drwxr-xr-x  11 huangsizhe  staff    352  4  9 00:57 国际化
-rw-r--r--   1 huangsizhe  staff   6745  6 22 15:57 定时任务.ipynb
-rw-r--r--   1 huangsizhe  staff   2643  6 22 16:12 调用子进程.ipynb
-rw-r--r--   1 huangsizhe  staff  11066  6 21 23:46 结构数据验证.ipynb
drwxr-xr-x   9 huangsizhe  staff    288  4  9 00:57 数据压缩与归档

使用asyncio来执行子进程

subprocess类似,asyncio有一个异步实现的subprocess

import asyncio
cmd = "ls -la"
proc = await asyncio.create_subprocess_shell(
    cmd,
    stdout=asyncio.subprocess.PIPE,
    stderr=asyncio.subprocess.PIPE
)

stdout, stderr = await proc.communicate()

print(f'[{cmd!r} exited with {proc.returncode}]')
if stdout:
    print(f'[stdout]\n{stdout.decode()}')
if stderr:
    print(f'[stderr]\n{stderr.decode()}')
['ls -la' exited with 0]
[stdout]
total 96
drwxr-xr-x  14 huangsizhe  staff    448  6 22 16:30 .
drwxr-xr-x  17 huangsizhe  staff    544  6  6 10:39 ..
-rw-r--r--@  1 huangsizhe  staff   6148  5 15 20:18 .DS_Store
drwxr-xr-x  14 huangsizhe  staff    448  6 21 23:49 .git
-rw-r--r--   1 huangsizhe  staff   1203  4  9 00:57 .gitignore
drwxr-xr-x   5 huangsizhe  staff    160  6 22 15:58 .ipynb_checkpoints
-rw-r--r--   1 huangsizhe  staff   1074  4  9 00:57 LICENSE
-rw-r--r--   1 huangsizhe  staff    494  5 15 18:50 README.md
drwxr-xr-x   2 huangsizhe  staff     64  4  9 01:40 img
drwxr-xr-x  11 huangsizhe  staff    352  4  9 00:57 国际化
-rw-r--r--   1 huangsizhe  staff   6745  6 22 15:57 定时任务.ipynb
-rw-r--r--   1 huangsizhe  staff   5042  6 22 16:30 调用子进程.ipynb
-rw-r--r--   1 huangsizhe  staff  11066  6 21 23:46 结构数据验证.ipynb
drwxr-xr-x   9 huangsizhe  staff    288  4  9 00:57 数据压缩与归档