【问题标题】:Python 3 : subprocess causes zombiesPython 3:子进程导致僵尸
【发布时间】:2018-06-06 03:47:38
【问题描述】:

按照the popular answers to this questionhere 的说明,我在python 3 中创建了以下代码:

  p1                = subprocess.Popen(["ps", "-e", "-o", "pcpu,args"],           stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  p2                = subprocess.Popen(["cut", "-c", "-132"],   stdin=p1.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  p3                = subprocess.Popen(["awk", "NR>2"],         stdin=p2.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  p4                = subprocess.Popen(["sort", "-nr"],         stdin=p3.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  p5                = subprocess.Popen(["head", "-10"],         stdin=p4.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  ps,err            = p5.communicate()
  psout             = str(ps, 'utf-8')

此代码在循环中每分钟左右调用一次。与我被引导相信这仍然会产生僵尸相反。 我究竟做错了什么?

编辑:运行代码时的僵尸:

$ ps -eo pid,ppid,state,cmd | awk '$3 == "Z"'
14441 11232 Z [ps] <defunct>
14442 11232 Z [cut] <defunct>
14445 11232 Z [sort] <defunct>

【问题讨论】:

  • 也添加相关的ps 输出。
  • 嗯...我实际上是指相关的僵尸 (Z) 进程。这里ps -eo pid,ppid,state,cmd | awk '$3 == "Z"'
  • 嗯。我的错。更改了最新的编辑以反映这一点。

标签: python subprocess zombie-process


【解决方案1】:

您需要为所有子进程使用communicate() 以摆脱“已失效”的进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 2016-07-03
    相关资源
    最近更新 更多