arnuschky arnuschky - 1 year ago 81
Python Question

Graceful exit server when using Django's autoreloader

I am using a custom Django

command that is supposed to run a bunch of cleanup functions upon termination. This works fine as long as I don't use the autoreloader: by server catches the KeyboardInterrupt exception properly and exits gracefully.

However, if I use Django's autoreloader, the reloader seems to simply kill the server thread without properly terminating it (as far as I can tell, it doesn't have any means to do this).

This seems inherently unsafe, so I can't really believe that there's not a better way of handling this.

Can I somehow use the autoreloader functionality without having my server thread be killed uncleanly?

Answer Source

Try using the atexit module to catch the termination. It should work for everything which acts like SIGINT or SIGTERM, SIGKILL cannot be interrupted (but should not be sent by any auto-restart script without sending SIGTERM before).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download