In my code I have function like this:
def myfunc():
# Don't do anything if there's an instance already
if get_var('running') == 'true':
return
set_var('running', 'true')
# In case things go wrong
atexit.register(set_var, 'running', 'false')
do_something()
do_something_else()
set_var('running', 'false')
# Unregister handler because nothing bad happened
atexit.unregister(set_var)
set_var
sets variable that is contained in database.
The purpose of all those set_var
s is to prevent multiple instances from running at the same time.
atexit
handler works fine when program is being interrupted by Ctrl-C but doesn't when it's killed by system or something like that.
I know about signal
but it doesn't allow to cancel handler.
How do I do that? Or how can change structure to achieve the same functionality?