当前位置: 首页 > news >正文

Python subprocess管理外部进程的完整实践

Python subprocess管理外部进程的完整实践

subprocess模块替代了os.system、os.popen等旧API。核心是Popen类,run和call是便捷封装。

Popen的基本用法:

import subprocess

proc = subprocess.Popen(['ls', '-la'], stdout=subprocess.PIPE)
output, errors = proc.communicate()
print(output.decode())

Popen启动子进程并立即返回。communicate等待子进程结束并收集输出。

run的便捷封装:

result = subprocess.run(['ls', '-la'], capture_output=True, text=True, check=True)
print(result.stdout)
print(result.returncode)

run等特子进程结束,返回CompletedProcess对象。capture_output=True自动捕获stdout和stderr。text=True以文本模式返回而非字节。

管道连接多个进程:

ls = subprocess.Popen(['ls', '-la'], stdout=subprocess.PIPE)
grep = subprocess.Popen(['grep', 'py'], stdin=ls.stdout, stdout=subprocess.PIPE)
ls.stdout.close()
output = grep.communicate()[0]
print(output.decode())

stdin=ls.stdout将ls的输出连接到grep的输入。ls.stdout.close()在父进程中关闭管道副本,避免死锁。

超时控制:

try:
result = subprocess.run(
['sleep', '10'],
timeout=5,
capture_output=True
)
except subprocess.TimeoutExpired:
print("Process timed out")

timeout=5设置超时。超时后杀死子进程并抛出TimeoutExpired。

环境变量控制:

import os

env = os.environ.copy()
env['MY_VAR'] = 'custom_value'

result = subprocess.run(
['printenv', 'MY_VAR'],
env=env,
capture_output=True,
text=True
)
print(result.stdout) # custom_value

env=dict提供子进程的完整环境变量。省略env继承当前进程的环境。

工作目录:

result = subprocess.run(
['pwd'],
cwd='/tmp',
capture_output=True,
text=True
)
print(result.stdout.strip()) # /tmp

cwd='/tmp'设置子进程的工作目录。

输入重定向:

result = subprocess.run(
['sort'],
input='banana\napple\ncherry\n',
capture_output=True,
text=True
)
print(result.stdout) # apple banana cherry (排序后)

input='string'将字符串作为子进程的stdin。

错误处理:

result = subprocess.run(
['false'],
capture_output=True
)
if result.returncode != 0:
print(f"Process failed with code {result.returncode}")

通过returncode检查进程退出状态。check=True时异常会在非零返回时抛出CalledProcessError。

shell注入防护:

# 正确:参数列表方式(无注入风险)
subprocess.run(['ls', '-la', user_input])

# 错误:字符串方式(有注入风险)
# subprocess.run(f'ls -la {user_input}', shell=True) # 危险

参数列表方式自动转义参数,shell=True时需手动处理user_input。

Popen的其他参数:

proc = subprocess.Popen(
['long_running_process'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0, # 无缓冲
universal_newlines=True, # 文本模式(Python 3.7+用text=True)
start_new_session=True, # 创建新进程组
creationflags=0 # Windows特定标志
)

start_new_session=True创建新的进程组,子进程及其子孙进程可以一起管理。

进程组管理:

import signal

proc = subprocess.Popen(
['long_running_script.sh'],
start_new_session=True,
)

# 杀死整个进程组
import os
pgid = os.getpgid(proc.pid)
os.killpg(pgid, signal.SIGTERM)

进程组确保子进程创建的孙进程也被终止。

异步Popen:

import asyncio

async def run_command(cmd):
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
return stdout, stderr, proc.returncode

asyncio.create_subprocess_exec使用exec方式(推荐),create_subprocess_shell使用shell方式。

资源限制:

import resource

def set_limits():
resource.setrlimit(resource.RLIMIT_CPU, (1, 1))
resource.setrlimit(resource.RLIMIT_AS, (100 * 1024 * 1024, 100 * 1024 * 1024))

proc = subprocess.Popen(
['some_command'],
preexec_fn=set_limits # Linux
)

preexec_fn在子进程中fork后exec前执行。可以设置资源限制(RLIMIT_CPU, RLIMIT_AS等)。

http://www.jsqmd.com/news/1020970/

相关文章:

  • SQL中IN操作符的执行原理与性能优化实战指南
  • 3D数据集剪枝:解决长尾分布与嵌入几何优化
  • SpringBoot+Vue BS老年人体检管理系统管理平台源码【适合毕设/课设/学习】Java+MySQL
  • yolo模型微调训练
  • 绍兴豆包推广公司评测:实力与服务维度对比解析 - 奔跑123
  • 2026年口碑好的布袋除尘器/江苏喷砂房除尘器长期合作厂家推荐 - 行业平台推荐
  • D2DX:三步解锁暗黑破坏神2高清宽屏体验,告别卡顿黑边
  • 凯撒旅业实力怎么样?在行业里排第几?从全产业链布局看其市场韧性 - 品牌2026
  • 设计 Token 系统建设:从颜色变量到设计决策的工程化体系
  • 二维二分法:结构化决策工具,从产品优先级到职业规划的应用
  • 【解决方案】Parsec VDD:突破物理限制的虚拟显示器技术实践
  • 17天300万流水:揭秘邀请退款模式
  • RK3566嵌入式视频开发实战:从硬件解码到AI推理全流程解析
  • 梯度下降法数学理解
  • Python abc抽象基类的虚拟子类机制
  • 2026年长沙、成都婚介市场观察:有实力的正规婚介公司如何甄别? - 优质品牌商家
  • 孪生空间精准映射 营区库区物资与仓储空间透明化管控
  • BetterNCM安装器终极指南:5分钟解锁网易云音乐插件系统
  • 通用Agentic RAG智能知识系统
  • 3步实现NVIDIA显卡免费升级:用FSR 3帧生成技术替代DLSS-G的完整指南
  • 魔兽争霸3终极增强指南:WarcraftHelper插件让你的游戏体验焕然一新
  • MuleSoft AI编排:企业级LLM集成的七层可审计架构
  • Python闭包与装饰器的高级陷阱
  • 2026水族用品什么牌子好?马印全品类覆盖进入候选 - 华旭传媒
  • Novel-downloader:可扩展通用型小说下载解决方案的技术架构解析
  • 2026年口碑好的超细粉选粉机/水泥磨选粉机/江苏立式选粉机/大型工业选粉机厂家哪家好 - 行业平台推荐
  • 东莞跨境电商培训机构排名:2026年最新评测 - 东莞选校指南
  • Sqribble:面向知识工作者的文档操作系统与自动化交付方案
  • PPG研究中暑的算法记录
  • 六顶点模型与高斯自由场的数学关联及收敛性分析