【发布时间】:2018-02-10 16:15:00
【问题描述】:
我有两个 python 文件:
a.py:
import subprocess, time, os, signal
myprocess = subprocess.Popen("b.py", shell=True)
time.sleep(2)
os.kill(myprocess.pid, signal.SIGTERM)
b.py:
import atexit
def cleanup():
print "Cleaning up things before the program exits..."
atexit.register(cleanup)
print "Hello world!"
while True:
pass
a.py 正在生成 b.py 并在 2 秒后终止进程。问题是我希望 cleanup 函数在它被杀死之前调用 b.py 但我无法让它工作。
我还在os.kill 函数中尝试了SIGKILL 和SIGINT,但对我都没有用。
当前输出(a.py):
Hello, World!
(2 seconds later, program ends)
预期输出(a.py):
Hello, World!
(2 seconds later)
Cleaning up things before the program exits...
(program ends)
【问题讨论】:
-
你在什么平台上?
-
Windows,但将来也可能是 Linux。
标签: python windows signals interrupt kill