czbiohub-sf / Rapid-QC-MS

Realtime quality control for mass spectrometry data acquisition
https://czbiohub-sf.github.io/Rapid-QC-MS
Other
15 stars 2 forks source link

MS-AutoQC jobs do not self-terminate upon completion #49

Closed wasimsandhu closed 1 year ago

wasimsandhu commented 2 years ago

The subprocess is started like so:

listener = psutil.Popen(["py", "AcquisitionListener.py", acquisition_path, str(filenames), run_id])

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 using listener.kill(). Simple!

wasimsandhu commented 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!