Python:使用subprocess.Popen执行长时间运行的进程,将其杀死,并访问其输出

时间:2018-11-29 08:48:28

标签: python multithreading subprocess

我试图以root身份(因为必须这样做)在线程中,在python中运行“长时间运行”的进程,然后将其杀死,然后访问其输出。

有问题的进程是“ babeld”,当我在终端中启动它时,它会在stdout上输出文本。但是,当我运行以下代码时,我无权访问stderr或stdout:

% ./example.py                                                                                                                                                                                                                                                                                   
Waiting for output
Pgid: 13445, pid: 13445
Stopping task
Permission Error!
Calling sudo kill 13445
B
None
None
End

代码:

#!/usr/bin/env python3

import subprocess
import threading
import time
import os

def main():
    task = TaskManager()
    task.launch()
    time.sleep(2)
    task.stop()

    print(task.stdout)
    print(task.stderr)


class TaskManager(threading.Thread):
    def __init__(self):
        super().__init__()
        self.start_event = threading.Event()
        self.stderr = None
        self.stdout = None
        self.pgid = None
        self.task = None
        self.start()


    def run(self):
        self.start_event.wait()
        self.task = subprocess.Popen(["sudo", "babeld", "-d", "2", "wlp2s0"],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     preexec_fn=os.setsid)
        self.pgid = os.getpgid(self.task.pid)
        print("Waiting for output")
        self.stdout, self.stderr = self.task.communicate()
        print("End")

    def launch(self):
        self.start_event.set()

    def stop(self):
        print("Pgid: %s, pid: %s" % (self.pgid, self.task.pid))
        try:
            print("Stopping task")
            self.task.terminate()
        except PermissionError:
            print("Permission Error!")
            print("Calling sudo kill %d" % self.pgid)
            subprocess.check_call(["sudo", "kill", str(self.pgid)])
            print("B")

if __name__ == '__main__':
    main()

如何在有权访问其stderr和stdout的同时正确终止以root用户身份运行的进程?

谢谢

1 个答案:

答案 0 :(得分:0)

食谱很简单:不要使用communicate。您可以将您的self.stdoutself.stderr类属性替换为以下getter:

@property
def stdout(self):
    return self.task.stdout.read()

@property
def stderr(self):
    return self.task.stderr.read()

顺便说一句,这种方法使您有机会拒绝代码中的线程使用。示例:

# !/usr/bin/env python3

import subprocess
import time
import os


def main():
    task = TaskManager()
    task.launch()
    time.sleep(2)
    task.stop()

    print(task.stdout)
    print(task.stderr)


class TaskManager:
    def __init__(self):
        self.pgid = None
        self.task = None

    @property
    def stdout(self):
        return self.task.stdout.read()

    @property
    def stderr(self):
        return self.task.stderr.read()

    def launch(self):
        self.task = subprocess.Popen(["sudo", "babeld", "-d", "2", "wlp2s0"],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     preexec_fn=os.setsid)
        self.pgid = os.getpgid(self.task.pid)
        print("Waiting for output")

    def stop(self):
        print("Pgid: %s, pid: %s" % (self.pgid, self.task.pid))
        try:
            print("Stopping task")
            self.task.terminate()
        except PermissionError:
            print("Permission Error!")
            print("Calling sudo kill %d" % self.pgid)
            subprocess.check_call(["sudo", "kill", str(self.pgid)])
            print("B")


if __name__ == '__main__':
    main()