基于类的干净使用解决方案:
import signalimport timeclass 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 = Trueif __name__ == '__main__': killer = GracefulKiller() while not killer.kill_now: time.sleep(1) print("doing something in a loop ...") print "End of the program. I was killed gracefully :)"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)