Django exit function

273 views
Skip to first unread message

Krešimir

unread,
Sep 10, 2016, 1:44:01 PM9/10/16
to Django users
Hello,

I have a turn_the_light_on() function that, well, turns on a light...
I put it in wsgi.py (as suggested by internet) just before application = get_wsgi_application()call.

Where can I put turn_the_light_off() function call to turn off the light when the application quits?

Thank you!

Michal Petrucha

unread,
Sep 10, 2016, 3:27:21 PM9/10/16
to django...@googlegroups.com
In short, there is no such thing. In the most common scenario
(including yours, based on your email), Django is used as a WSGI
application, which starts up once, and then lives inside an
application server, which listens to requests, and translates them
into function calls. In that sense, Django is more of a passive thing,
that waits to be called with a request, which it then dutifully
processes, and finally enters the waiting state again. This happens
until the application server itself exits, but that is out of the
scope of what Django does.

Usually, application servers also just run indefinitely, until they
are killed. Or you can configure them to only run for a certain amount
of time. Or you can set them up to quit and restart after they process
a certain number of requests. Either way, the shutdown sequence
usually consists of just terminating the process.

What you are asking for sounds like it falls into the responsibility
of your application server. For example, Gunicorn has an ``on_exit``
hook [1], or if you're using uwsgi, it also has some hooks [2], though
I haven't been able to extract much useful information at a glance,
but you might be able to find something that suits your need.

Cheers,

Michal


[1]: http://docs.gunicorn.org/en/latest/settings.html#on-exit
[2]: https://uwsgi-docs.readthedocs.io/en/latest/Hooks.html
signature.asc

Krešimir

unread,
Sep 14, 2016, 9:00:49 PM9/14/16
to Django users
Thank you, dear Sir!

I have tried as-user-atexit and it does not work with Ctrl+c event. Don't know about other exit events since my machine powers the light in question. So the light goes off when machine goes off anyway.

Maybe if I constantly check fo existance of some django system process?

Erik Cederstrand

unread,
Sep 14, 2016, 9:52:52 PM9/14/16
to Django Users

> Den 14. sep. 2016 kl. 23.00 skrev Krešimir <kresimir....@gmail.com>:
>
> Thank you, dear Sir!
>
> I have tried as-user-atexit and it does not work with Ctrl+c event. Don't know about other exit events since my machine powers the light in question. So the light goes off when machine goes off anyway.

You *might* be able to wrap the UWSGI event loop to catch KeyboardInterrupt, and set up a signal handler for SIGTERM, SIGABRT etc, but it seems a wee bit overkill for your needs.

> Maybe if I constantly check fo existance of some django system process?

That would be the simpler and more fool-proof solution. Use cron if you can live with 1-minute resolution, otherwise use one of the many available service monitoring tools to check if your WSGI server is running and call turn_the_light_off() if not.

Erik
Reply all
Reply to author
Forward
0 new messages