简体   繁体   中英

Gracefully Terminate Child Python Process On Windows so Finally clauses run

On Windows boxes, I have a number of scenarios where a parent process will start a child process. For various reasons - the parent process may want to abort the child process but (and this is important) allow it to clean up - ie run a finally clause:

try:
  res = bookResource()
  doStuff(res)
finally:
  cleanupResource(res)

(These things may be embedded in contexts like the closer - and generally are around hardware locking/database state)

The problem is that I'm unable to find a way to signal the child in Windows (as I would in a Linux environment) so it would run the clean up before terminating. I think this requires making the child process raise an exception somehow (as the Ctrl-C would).

Things I've tried:

  • os.kill
  • os.signal
  • subprocess.Popen with creationFlags and using ctypes.windll.kernel32.GenerateConsoleCtrlEvent(1, p.pid) abrt signal. This requires a signal trap and inelegant loop to stop it immediately aborting.
  • ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, p.pid) - ctrl-c event - did nothing.

Has anyone got a surefire way of doing this, so that the child process can clean up?

I was able to get the GenerateConsoleCtrlEvent working like this:

import time
import win32api
import win32con
from multiprocessing import Process


def foo():
    try:
        while True:
            print("Child process still working...")
            time.sleep(1)
    except KeyboardInterrupt:
        print "Child process: caught ctrl-c"

if __name__ == "__main__":
    p = Process(target=foo)
    p.start()
    time.sleep(2)

    print "sending ctrl c..."
    try:
        win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, 0)
        while p.is_alive():
            print("Child process is still alive.")
            time.sleep(1)
    except KeyboardInterrupt:
        print "Main process: caught ctrl-c"

Output

Child process still working...
Child process still working...
sending ctrl c...
Child process is still alive.
Child process: caught ctrl-c
Main process: caught ctrl-c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM