I have an application that launches a daemon like this:

directory_cleaner = DirectoryCleaner() directory_cleaner.daemon = True directory_cleaner.start() 

The problem is that when I launch gunicorn , I get a lot of processes with a lot of demons. Is there any opportunity to gunicorn information to gunicorn about which of the processes should start the daemon in a separate thread, while other processes should not do this?

Maybe there are other ways? I don't want to run separate cron 'scripts either. I want to keep a single launch point for the entire program.

  • Oh, and so far no one has even written a comment? In my opinion, the demon should be launched separately from gunicorn at the same time as it, and when it is necessary to do some work, then send the command to the demon in any available way (if on a schedule, then cron is better, because gunicorn is for this is not intended) - andreymal
  • I just don't want the program to crumble into pieces. Some part is started by cron 'om, some supervisоrd , gunicorn , etc. Want to click run , so that everything is ready and everything. :) - faoxis
  • Then refuse gunicorn :) Well, either launch it and the demon with a separate special script with this very run - andreymal
  • Is hopelessness? ; ( - faoxis
  • one
    association: stackoverflow.com/questions/24101724/… - Nicolas Chabanovsky

1 answer 1

You need to store the state of the daemon somewhere, and also take measures to start the daemon with several processes at the same time.

The situation is complicated by the fact that inside gunicorn there are no primitives to solve this problem. So you need to contact any third-party service in each process to find out if the cleaning daemon is running or not. If the daemon is not running, then the responsible process needs to protect some storage from the state of the daemon (for example, using flock ), then start the cleaning daemon, wait for the successful launch, write the state to the global variable , unlock.

 import simpleflock with simpleflock.SimpleFlock("/tmp/dircleaner"): # Запускаем демона # Отмечаем в файле или в БД что демон запущен pass 

In the worst case, you will, in fact, in one part of the program, limit yourself to running into just one thread at the time the daemon starts. This means that all processes on the server will wait for one until he reports on the successful launch of the daemon. Bonus to this solution are various options for race condition and other heisenbags.

In addition, you need somewhere to track the correct operation of the purge daemon. What if it falls with an error because the disk has run out of logs? What if the parent process is killed by an OOM Killer ? What if ...? This is a whole separate problem that they know how and how to solve such programs as Supervisor.

Is it really necessary to hammer nails with a screwdriver?