首先,我不确定您是否需要第二个线程来设置shutdown_flag.
为什么不直接在 SIGTERM 处理程序中设置呢?
另一种方法是从处理程序中引发异常,该异常SIGTERM将向上传播到堆栈。假设您已经进行了适当的异常处理(例如使用with/contextmanager和try: ... finally:块),这应该是一个相当优雅的关闭,类似于Ctrl+C您的程序。
示例程序signals-test.py:
#!/usr/bin/python
from time import sleep
import signal
import sys
def sigterm_handler(_signo, _stack_frame):
# Raises SystemExit(0):
sys.exit(0)
if sys.argv[1] == "handle_signal":
signal.signal(signal.SIGTERM, sigterm_handler)
try:
print "Hello"
i = 0
while True:
i += 1
print "Iteration #%i" % i
sleep(1)
finally:
print "Goodbye"
现在看看Ctrl+C行为:
$ ./signals-test.py default
Hello
Iteration #1
Iteration #2
Iteration #3
Iteration #4
^CGoodbye
Traceback (most recent call last):
File "./signals-test.py", line 21, in <module>
sleep(1)
KeyboardInterrupt
$ echo $?
1
这次我SIGTERM在 4 次迭代后发送它kill $(ps aux | grep signals-test | awk '/python/ {print $2}'):
$ ./signals-test.py default
Hello
Iteration #1
Iteration #2
Iteration #3
Iteration #4
Terminated
$ echo $?
143
这次我启用我的自定义SIGTERM处理程序并发送它SIGTERM:
$ ./signals-test.py handle_signal
Hello
Iteration #1
Iteration #2
Iteration #3
Iteration #4
Goodbye
$ echo $?
0