Wednesday, 26 December 2018

Start Django config app only once when using multiple Gunicorn workers

I'm using :

  • python 3.6
  • django==2.1.1
  • gunicorn==19.9.0

i have done the following:

  • created a django project called api
  • created an apiapp (an app in my project)

and i have this code in api_app's apps.py :

from django.apps import AppConfig
from api import settings

class Apiapp(AppConfig):
    name = 'apiapp'
    verbose_name = "random_name"

    def ready(self):
        self.job()


    @classmethod
    def job(cls):
        ### doing whatever here for example :
        print(settings.SHARED_VARIABLE)

and the following in api_app's __init__.py:

import os
default_app_config = 'linkedin.apps.LinkedinConfig'

i'm creating an API so i am required to use multiple workers when deploying:

gunicorn api.wsgi -w 10

now, my issue is that the function job which is called when the server is started, is getting called 10 times because i'm using 10 gunicorn workers, i would like to call it only once

another thing that i would like to do is to have the settings.SHARED_VARIABLE variable, shared between the different workers. this variable will be updated only by the worker that will launch the app.py on server start.

Thank you !



from Start Django config app only once when using multiple Gunicorn workers

No comments:

Post a Comment