1

Implemented a singleton using Method 1 from this question:

def singleton(class_):
  instances = {}
  def getinstance(*args, **kwargs):
    if class_ not in instances:
        instances[class_] = class_(*args, **kwargs)
    return instances[class_]
  return getinstance

@singleton
class MyClass(BaseClass):
  pass

This works locally when I'm running it locally, but when I deploy it with gunicorn and django-crontabs it appears as if the singleton doesn't hold up and multiple instances of the class are instantiated. I'm wondering if each gunicorn worker spawns a separate instance of the class. In short, I'm asking about the interactions with Python and Django when running a web application with gunicorn.

Community
  • 1
  • 1
Rawr
  • 2,066
  • 2
  • 21
  • 44

1 Answers1

3

Yes, each process will have its own memory space and therefore its own instances. If you need to share data, you should use the database or cache.

Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786