Python subprocess: callback when cmd exits

后端 未结 7 1625
暗喜
暗喜 2020-11-28 21:36

I\'m currently launching a programme using subprocess.Popen(cmd, shell=TRUE)

I\'m fairly new to Python, but it \'feels\' like there ought to be some api

7条回答
  •  暖寄归人
    2020-11-28 22:32

    You're right - there is no nice API for this. You're also right on your second point - it's trivially easy to design a function that does this for you using threading.

    import threading
    import subprocess
    
    def popen_and_call(on_exit, popen_args):
        """
        Runs the given args in a subprocess.Popen, and then calls the function
        on_exit when the subprocess completes.
        on_exit is a callable object, and popen_args is a list/tuple of args that 
        would give to subprocess.Popen.
        """
        def run_in_thread(on_exit, popen_args):
            proc = subprocess.Popen(*popen_args)
            proc.wait()
            on_exit()
            return
        thread = threading.Thread(target=run_in_thread, args=(on_exit, popen_args))
        thread.start()
        # returns immediately after the thread starts
        return thread
    

    Even threading is pretty easy in Python, but note that if on_exit() is computationally expensive, you'll want to put this in a separate process instead using multiprocessing (so that the GIL doesn't slow your program down). It's actually very simple - you can basically just replace all calls to threading.Thread with multiprocessing.Process since they follow (almost) the same API.

提交回复
热议问题