杀死来自不同线程的子进程

时间:2016-02-03 11:18:01

标签: python multithreading subprocess

我遇到一个问题,就是在程序退出时从另一个线程中杀死一个衍生的子进程。我甚至尝试过atexit.register但没有帮助。请看我是python的新手。请参阅下面的代码

bgp = BigGrapePines()
bs = BigSourers()
    def run():
        proc = subprocess.Popen("java MyAppPerformStunts", stdout=outfile, stderr=subprocess.STDOUT,
                                                cwd=self.workingdirectory, shell=True)   
        proc.wait()
        return proc 
#----------------------------
    def callfunc(a,b):
        procobj = bs.run() #Going to take hrs to complete
        print("Process PID is given below")
        print(procobj.pid)
        bgp.setppid(procobj.pid)

    def cleanup():
        prid = bgp.getppid()
        if prid is None:
            pass
        else:
            try:
                os.kill(prid,signal.SIGTERM)
            except OSError:
                print("Cannot Kill the Process" + str(prid))
        pass


    def main(): 
        thread = Thread(target = callfunc, args = ("Latest","1"))
        thread.daemon = True
        thread.start()
        time.sleep(20)
        performoperations();
        cleanup()


if __name__ == "__main__":
    main()      

performoperations()将运行几分钟。在完成主线程时,我想杀死在run()上打开的子进程。由于我将pid作为run()的返回值,我无法继续进行     任何帮助都会受到高度赞赏

0 个答案:

没有答案