def do_stuff_before_python_terminates():
save_variables_in_mysql()
do_this_and_that()...
def main():
do stuff
while loops ect...
def sigterm(x, y):
raise Exception()
def sigint(signal, frame):
raise Exception()
signal.signal(signal.SIGINT, sigint)
signal.signal(signal.SIGTERM, sigterm)
try:
while True:
main()
except Exception as e:
logger.error("Exception")
do_stuff_before_python_terminates()
logger.log("sys.exit")
sys.exit(0)
I use Python in a Docker Container
When i do ctrl+c via terminal tty or stop the image python does stop and do it not always successfully "do_stuff_before_python_terminates()".
The reason is that when python is randomly in a while loop then i don't have luck it does not exit it stays in the while loop and do still other stuffs and do not terminate successfully.
Docker only waits 10 seconds before it kills the container and than voila it does not "do_stuff_before_python_terminates()"
what am i doing here wrong, how to solve the problem that even when its in a while loop it instantly exits and "do_stuff_before_python_terminates()"
Updated Explanation:
if python threws an error
it does successfully jump to
except Exception as e:
logger.error("Exception")
do_stuff_before_python_terminates()
logger.log("sys.exit")
sys.exit(0)
if i stop the container or use ctrl+c and python is NOT in a while loop
it does successfully jump to
sigterm() or sigint() -> then raise an Exception() -> then jumps to
except Exception as e:
logger.error("Exception")
do_stuff_before_python_terminates()
logger.log("sys.exit")
sys.exit(0)
if i stop the container or use ctrl+c and python is IN an Loop
it does stay in the loop
do stuff
do stuff
do stuff
after nearly 20-30seconds
sigterm() or sigint() -> then raise an Exception() -> then jumps to
except Exception as e:
logger.error("Exception")
do_stuff_before_python_terminates()
logger.log("sys.exit")
sys.exit(0)
what i need is, that it does instantly jumps out the while loop whenever i stop the container or do ctrl+c (sigterm + sigint)
sigterm() or sigint() -> then raise an Exception() -> then jumps to
except Exception as e:
logger.error("Exception")
do_stuff_before_python_terminates()
logger.log("sys.exit")
sys.exit(0)
Docker does kill the container after 10seconds so, python only has 10 seconds to exit, the effect is that it does never do_stuff_before_python_terminates() when python is in a loop