pythoneventsexitatexit

Python: How to make the function run unconditionally when the program ends?


I must ensure that the end_event() function is executed at the end of the program. I tried to implement this as Python's atexit. However, when the .py file was converted to an exe file with a PyInstaller and closed with a click, it did not work. I would appreciate it if you could tell me a solution that always works. Have a good day.

import atexit
import signal
import pyupbit

def end_event():
    for keys in buy_list.keys():
        order = upbit.get_order(keys)
        if "state" in order:
            if(order['state'] == 'wait'):
                upbit.cancel_order(keys)
    exit(1)

atexit.register(end_event)
signal.signal(signal.SIGINT, end_event)

Solution

  • There is no way to guarantee that code will run when the process exits, because there are ways to exit which do not permit any code to run.

    Patterns for emergency cleanup.

    The best pattern is: Never be in a state which requires emergency cleanup.

    See also: What does the POSIX standard say about thread stacks in atexit() handlers? What's the OS practice?