1

I'm using uwsgi-emperor on Debian 8 in my production systems. For a specific Django project, I need to run a computing intensive setup task only once at the launch of the vassal. The vassal can have multiple workers/threads, but the task must be executed only one time, no matter how many workers/threads are spawned.

Currently, I'm executing this setup task every time a new worker is launched, but this is clearly suboptimal. The setup task is an invocation of a method from the same Django project, but I think that doesn't change the problem.

Is there any way of doing this from uWSGI?

  • Have you tried the proposal posted here: [http://stackoverflow.com/questions/6791911/execute-code-when-django-starts-once-only](http://stackoverflow.com/questions/6791911/execute-code-when-django-starts-once-only)? – Rafa He So Dec 19 '16 at 14:17
  • @rafa-he-so Yes, basically, that's the same approach I'm using. When working on a production server with multiple workers, the initialization code runs several times, and that's is what I want to avoid – Mario J. Barchéin Dec 19 '16 at 17:14

1 Answers1

0

You could try to use a singletone approach, this code at settings.py would call the startup_only_once() function only once:

from tendo.singleton import SingleInstance


def startup_only_once():
    print("One time only")


try:
    FIRST_START = SingleInstance()
    startup_only_once()
except:
    pass
Rafa He So
  • 473
  • 3
  • 12