How to kill a subprocess initiated by a different function in the same class
daemon, python, subprocess
Solution
jterrace code works. If you don't want it to start when you initialize, just call Popen in a separate function and pass nothing to the init function
import subprocess
import time
class foo_class(object):
def __init__(self):
pass
def start(self):
self.foo = subprocess.Popen(['a daemon service'])
def stop(self):
self.foo.kill()
self.foo.wait() #don't know if this is necessary?
def restart(self):
self.start()
foo = foo_class()
foo.start()
time.sleep(5)
foo.stop()
Problem
I want to kill a process from another function in the class attending to the fact that it was initiated by another function. Here's an example: ``` import time class foo_class: global foo_global def foo_start(self): import subprocess self.foo_global =subprocess.Popen(['a daemon service']) def foo_stop(self): self.foo_start.foo_global.kill() self.foo_start.foo_global.wait() foo_class().foo_start() time.sleep(5) foo_class().foo_stop() ``` How should I define foo_stop?