1

I'm running apache with django and mod_wsgi enabled in 2 different processes.

I read that the second process is a on-change listener for reloading code on change, but for some reason the ready() function of my AppConfig class is being executed twice. This function should only run once.

I understood that running django runserver with the --noreload flag will resolve the problem on development mode, but I cannot find a solution for this in production mode on my apache webserver.

I have two questions:

  1. How can I run with only one process in production or at least make only one process run the ready() function ?

  2. Is there a way to make the ready() function run not in a lazy mode? By this, I mean execute only on on server startup, not on first request.

For further explanation, I am experiencing a scenario as follows: The ready() function creates a folder listener such as pyinotify. That listener will listen on a folder on my server and enqueue a task on any changes. I am seeing this listener executed twice on any changes to a single file in the monitored directory. This leads me to believe that both processes are running my listener.

Adam Link
  • 2,783
  • 4
  • 30
  • 44
Bat-chen
  • 88
  • 4

2 Answers2

2

No, the second process is not an onchange listener - I don't know where you read that. That happens with the dev server, not with mod_wsgi.

You should not try to prevent Apache from serving multiple processes. If you do, the speed of your site will be massively reduced: it will only be able to serve a single request at a time, with others queued until the first finishes. That's no good for anything other than a toy site.

Instead, you should fix your AppConfig. Rather than blindly spawning a listener, you should check to see if it has already been created before starting a new one.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

You shouldn't prevent spawning multiple processes, because it's good thing, especially on production environment. You should consider using some external tool, separated from django or add check if folder listening is already running (for example monitor persistence of PID file and it's content).

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • this is what I ended up doing. I fixed my AppConfig to support multi processes running it, but it still seems so bizzar to me that there is no way to run code only once on server startup. I had to use all kinds of patches to make sure that'll happen – Bat-chen Feb 07 '15 at 15:44
  • In gunicorn or uWSGI deployment there is always start script that you can use to run one-time startup code. There is no such possibility with mod_wsgi because it's up to apache to spawn wsgi processes. Also there is no master django thread by design, so each worker depends only on database server, not on each other. This is good solution always to check if your listener isn't already running by it's pid file so you can always be sure it is running only once. – GwynBleidD Feb 08 '15 at 16:58
  • @Bat-chen How did you achieve that? I am trying all kinds of patches and none works... – JulienD Apr 25 '16 at 12:56