将Python放置在docker容器中,优雅地停止

时间:2018-12-19 13:51:34

标签: python windows docker

我正在 Windows docker容器中运行一个非常基本的Python循环示例,我想正常停止

该脚本以这种方式在我的dockerfile中启动:

  

CMD [“ python.exe”,“ ./test.py”]

在Docker文档中,有一个 SIGTERM信号已发送到主命令,所以我试图以这种方式捕获它:

import signal
import time
import logging, sys

class GracefulKiller:
  kill_now = False
  def __init__(self):
    signal.signal(signal.SIGINT, self.exit_gracefully)
    signal.signal(signal.SIGTERM, self.exit_gracefully)

  def exit_gracefully(self,signum, frame):
    self.kill_now = True

if __name__ == '__main__':
  logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
  killer = GracefulKiller()
  while True:
    time.sleep(1)
    logging.info("doing something in a loop ...")
    if killer.kill_now:
      break

  logging.info("End of the program. I was killed gracefully :)")

理论上,信号应该被处理程序捕获,布尔值应该切换,循环应该退出并显示我的最后一条日志行。并非如此,只是在发出信号的那一刻(或2-3秒后)停止整个过程

C:\Users\Administrator\Documents\Projects\test>docker-compose up
Recreating test_1 ... done
Attaching to test_1
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
test_1  | INFO:root:doing something in a loop ...
Gracefully stopping... (press Ctrl+C again to force)
Stopping test_1   ... done

我的最后一行日志从未达到。 有人知道发生了什么吗?是特定于python的问题,特定于docker还是Windows特定?

我也尝试用docker日志检查停止的容器,最后一个日志也不在这里。试图在此之后添加睡眠,结果相同。

谢谢

1 个答案:

答案 0 :(得分:0)

只需抓住KeyboardInterrupt就可以了。

if __name__ == '__main__':
  logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
  try:
    while True:
      time.sleep(1)
      logging.info("doing something in a loop ...")
  except KeyboardInterrupt as ex:
    print('goodbye!')