kill subprocess when python process is killed?

后端 未结 3 529
自闭症患者
自闭症患者 2021-01-05 17:17

I am writing a python program that lauches a subprocess (using Popen). I am reading stdout of the subprocess, doing some filtering, and writing to stdout of main process.

3条回答
  •  再見小時候
    2021-01-05 17:49

    Windows doesn't have signals, so you can't use the signal module. However, you can still catch the KeyboardInterrupt exception when Ctrl-C is pressed.

    Something like this should get you going:

    import subprocess
    
    try:
        child = subprocess.Popen(blah)
        child.wait() 
    
    except KeyboardInterrupt:
        child.terminate()
    

提交回复
热议问题