0

我正在尝试通过 popen 在服务器监视器上 grep 连续流 5 秒,捕获输出并对其执行一些操作。我能够做到这一点,但 Popen 打开的子进程会一直保持并持续运行(因为它是一个监视器流)并影响我的 CPU 统计信息。下面是我用来运行的。

class ThreadWorker(threading.Thread):
def __init__(self, callable, *args, **kwargs):
    super(ThreadWorker, self).__init__()
    self.callable = callable
    self.args = args
    self.kwargs = kwargs
    self.setDaemon(True)

def run(self):
    try:
        self.callable(*self.args, **self.kwargs)
    #except wx.PyDeadObjectError:
        #pass
    except Exception, e:
        print e

if __name__ == "__main__":

def worker(pipe,fileName,grabtime):
    f = csv.writer(open(fname,'wb'),delimiter='\t',quoting=csv.QUOTE_ALL)

    while True:
        line = pipe.readline()
        csvlist = [line.rstrip(),int(grabtime)]
        f.writerow(csvlist)


fname = 'test.csv'

proc = Popen(mycmdhere, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)

try:
stdout_worker = ThreadWorker(worker, proc.stdout, fname,time.time())
stdout_worker.start()
stdout_worker.join(5)
except Exception, msg:
    print "Timed out!"

那是我的代码。试图杀死连续流的任何帮助都会很棒。

4

0 回答 0