docker容器中的Python,正常停止 [英] Python inside docker container, gracefully stop

查看:607
本文介绍了docker容器中的Python,正常停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running a very basic example of Python loop inside a Windows docker container, that I would like to gracefully stop.

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

The script is started this way in my dockerfile:


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

CMD [ "python.exe" , "./test.py"]

在docker文档中,它说是发送了 SIGTERM信号到主命令,所以我试图以这种方式捕获它:

In the docker documentation it's said a SIGTERM signal is sent to the main command, so I'm trying to catch it this way:

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秒后)停止整个过程

In theory the signal should be caught by the handler, the boolean should toggle and the loop should exit and display my very last log line. It doesn't, it's just stopping the whole thing at the moment the signal is emitted (or 2-3 seconds later rather)

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?

My last line log is never reached. Does anyone knows what's going on ? Is it a python specific issue, docker specific or Windows specific?

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

Also I tried to inspect the stopped container with docker logs, the last log isn't here either. Tried to add a sleep after it, same result.

谢谢

推荐答案

只需捕获 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!')

这篇关于docker容器中的Python,正常停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆