Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to run a function when server exits?

Tags:

python

django

I am writing a Django project where several processes are opened using Popen. Right now, when the server exits, these processes are orphaned. I have a function to terminate these processes, and I wish to organise it so that this function is called automatically when the server quits.

Any help would be greatly appreciated.

like image 541
GoldenBadger Avatar asked Apr 25 '15 15:04

GoldenBadger


1 Answers

Since you haven't specified which HTTP server you are using (uWSGI, nginx, apache etc.), you can test this recipe out on a simple dev server.

What you can try is to register a cleanup function via atexit module that will be called at process termination. You can do this easily by overriding django's builtin runserver command.

Create a file named runserver.py and put that in $PATH_TO_YOUR_APP/management/commands/ directory.

Assuming PROCESSES_TO_KILL is a global list holding references to orphan processes that will be killed upon server termination.

import atexit
import signal
import sys

from django.core.management.commands.runserver import BaseRunserverCommand

class NewRunserverCommand(BaseRunserverCommand):
    def __init__(self, *args, **kwargs):
        atexit.register(self._exit)
        signal.signal(signal.SIGINT, self._handle_SIGINT)
        super(Command, self).__init__(*args, **kwargs)

    def _exit(self):
        for process in PROCESSES_TO_KILL:
            process.terminate()

    def _handle_SIGINT(signal, frame):
        self._exit()
        sys.exit(0)

Just be aware that this works great for normal termination of the script, but it won't get called in all cases (e.g. fatal internal errors).

Hope this helps.

like image 148
Ozgur Vatansever Avatar answered Sep 28 '22 16:09

Ozgur Vatansever