Closed wasimsandhu closed 1 year ago
def listener_is_running(pid):
"""
Check if acquisition listener subprocess is still running
"""
time.sleep(1)
try:
if psutil.Process(pid).status() == "running":
return True
else:
return False
except Exception as error:
print("Error searching for subprocess using given pid", error)
def kill_acquisition_listener(pid):
"""
Kill acquisition listener subprocess using the pid
"""
try:
return psutil.Process(pid).kill()
except Exception as error:
print("Error killing acquisition listener:", error)
# Example usage
script = os.path.join(os.getcwd(), "src", "ms-autoqc", "Test.py")
listener = psutil.Popen(["python3", script])
pid = listener.pid
print(pid)
time.sleep(1)
kill_acquisition_listener(pid)
time.sleep(3)
print(listener_is_running(pid))
Special thanks to ChatGPT!
The subprocess is started like so:
But because it's in a callback, the listener object is destroyed unless saved. Strategy will probably be to store the process ID –
listener.pid
– in the database and kill it usinglistener.kill()
. Simple!