Can the Django development server be restarted with a signal?

I would like to be able to restart the manage.py runserver Django command using a signal (like in kill -HUP PID). Does Django even support this? SIGHUP, SIGINT, SIGTERM just exit the process.

Tried pkill -HUP, didn't work.

Can the Django development server be restarted with a signal?

The short answer is No.

The longer answer is that Django per se doesn't have any builtin support for POSIX signal handling (specifically SIGHUP) so the behavior will be Python's default signal handling behavior.

(Django signals are a completely different mechanism, and not relevant here.)

Alternatives:

  • The simple way to get a dev server (i.e. manage.py runserver) to restart is to touch one of your app's source files.

  • In production, you would typically be running multiple django worker process from a gunicorn server. In that case, you can individually kill the worker processes with a signal, and gunicorn will respawn it. Alternatively you can send a HUP signal to the gunicorn process and it will start new workers (with the latest code / configs) and then gracefully shutdown the old workers.

  • I guess if you were really adventurous you could try adding a Python signal handler for SIGHUP, and try to get it to restart the django framework within the process1.

References:


1 - How? I don't know! Maybe look at what the "restart-on-code-change" mechanism does? Put on your adventurer's gear, grab your machette and head out into the jungle ...

Вернуться на верх