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 usingctypes.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?