Search code examples
pythonsignalsatexit

Execute code at exit


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_vars 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?


Solution

  • I think I figured it out.

    # Used to check if myfunc is running in current program
    running_here = False
    
    # Set 'running' variable inside database to 'false' if myfunc was running
    # inside current program at the time of exiting or don't do anything otherwise
    atexit.register(lambda: set_var('running', 'false') if running_here else None)
    # Call atexit handler when SIGTERM is recieved by calling sys.exit
    signal.signal(signal.SIGTERM, lambda x, frame: sys.exit(0))
    
    def myfunc():
        global running_here
    
        # Don't do anything if there's an instance already
        if get_var('running') == 'true':
             return
    
        # Don't let multiple instances to run at the same time
        set_var('running', 'true')
        running_here = True
    
        do_something()
        do_something_else()
    
        # Allow other instances to run
        set_var('running', 'false')
        running_here = False
    

    All I needed to do was just to make a handler that would not need to be canceled over and over again. I did that by adding global variable running_here.

    When program is being terminated handler just checks if function is running in current program by checking running_here and if it's True then handler just sets variable running inside the database to 'false' so other instances won't start. If running_here is False it means that myfunc is not running and there's no need to reset running variable so it just quits.