我需要访问proc / pid / status并获取进程的VmPeak。对于更长的进程,我能够使用另一个线程获取它但是对于小进程我总是得到一个错误(没有这样的进程)。有什么办法吗?或者我可以用来完成工作的解决方法。我正在使用python 2.7,到目前为止我已经完成了以下工作..
import os
import subprocess
import threading
from shutil import copyfile
import time
rp = None
br = 0
pid = -1
class MemoryThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global br, rp
while br == 0:
try:
pid = rp.pid
except Exception as e:
pid = -1
if not pid == -1:
try:
pp = open("/proc/" + str(pid) + "/status")
st = "X"
for l in pp.readlines():
if l.startswith("State:"):
st = l.split(":", 1)[1].strip().split(' ')[0]
if st == "R":
copyfile("/proc/" + str(pid) + "/status", "status.txt")
print "copied"
pp.close()
except Exception as ee:
print ee
th = MemoryThread()
th.start()
cmd = "/home/mursalin/Desktop/A"
cmd = cmd.split(" ")
path_stdout = "/home/mursalin/Desktop/stdout.txt"
path_stderr = "/home/mursalin/Desktop/stderr.txt"
path_stdin = "/home/mursalin/Desktop/inp.txt"
rp = subprocess.Popen(cmd, stdin=open(path_stdin, "r"), stdout=False, stderr=open(
path_stderr, "w"), shell=False, preexec_fn=False)
rp.communicate()
br = 1
答案 0 :(得分:0)
您可能希望子进程保持僵尸状态,您可以在其中收集进程状态。但是subprocess.call
会自动收集僵尸进程,阻止子进程处于僵尸状态。
因此,您应该使用较低的api,如os.spawn
和os.popen
来解决问题。