Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run startup code in the parent with Django and Gunicorn

I need my code run at Django application startup, before Django starts listening for incoming connections. Running my code upon the first HTTP request is not good enough. When I use Gunicorn, my code must run in the parent process, before it forks.

https://stackoverflow.com/a/2781488/97248 doesn't seem to work in Django 1.4.2: it doesn't run the Middleware's __init__ method until the first request is received. Ditto for adding code to urls.py.

A quick Google search didn't reveal anything useful.

like image 944
pts Avatar asked Oct 23 '25 19:10

pts


2 Answers

This is old but in Gunicorn version 19.0 and above, you can create a custom script to run your application and include the startup code that you need there. Here is an example script using a django app:

#!/usr/bin/env python

"""
Script for running Gunicorn using our WSGI application
"""

import multiprocessing

from gunicorn.app.base import BaseApplication

from myapp.wsgi import application  # Must be imported first


class StandaloneApplication(BaseApplication):
    """Our Gunicorn application."""

    def __init__(self, app, options=None):
        self.options = options or {}
        self.application = app
        super().__init__()

    def load_config(self):
        config = {
            key: value for key, value in self.options.items()
            if key in self.cfg.settings and value is not None
        }
        for key, value in config.items():
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.application


if __name__ == '__main__':
    gunicorn_options = {
        'bind': '0.0.0.0:8080',
        'workers': (multiprocessing.cpu_count() * 2) + 1,
    }

    # Your startup code here

    StandaloneApplication(application, gunicorn_options).run()

like image 165
Kandil Avatar answered Oct 26 '25 07:10

Kandil


I have just run into this problem myself, and the solution was to basically chain the commands to guarantee execution and correct order:

Script started by systemd, supervisord or any other such system:

#!/bin/sh
python manage.py my_custom_command && gunicorn project.wsgi $@

Create your own custom django command and off you go. You can get some speedups if you disable sanity checks in the command (requires_system_checks and requires_migrations_checks set to False).

To make things more generic, you could create a "boot" signal to which you connect arbitrary functions, and just emit the signal from this custom command.

like image 42
Anonymous Avatar answered Oct 26 '25 07:10

Anonymous