Reputation: 999
I am using Django (1.9) and I'd like to share class object between management commands.
Let's say - to be simple - that I have a main app which instanciates some classes.
my_main_app/commands/mananagement/setup.py
#!/usr/bin/env python
from django.core.management import base, call_command
from somewhere import SingletonClass
class Command(base.BaseCommand):
'''
Overloads runserver command by creating a few extra objects
'''
def handle(self, *args, **options):
myObject = SingletonClass(date = datetime.now())
call_command("runserver") # this doesn't return
What I want is to access the "myObject" object within another command line call. For instance, I'd like to know when the myObject object was instanciated.
my_other_app/commands/mananagement/command.py
#!/usr/bin/env python
from django.core.management import base, call_command
from somewhere import SingletonClass
class Command(base.BaseCommand):
def handle(self, *args, **options):
myObject = SingletonClass() # <- this should be the same instance than the object created in the main app
return myObject.date # must return the date of the call to the setup command
In the example I use Singleton pattern because it seems to be close to what I want.
So far I found only the following solution:
1) The main app creates a server listening to command line calls
from somewhere import SingletonClass, Server
class Command(base.BaseCommand):
def handle(self, *args, **options):
self.myObject = SingletonClass(date = datetime.now())
self.__server = Server(handler = self.handle_distant)
self.__server.start() # this starts a listening server
def handle_distant(self, *args, **kwargs):
'''
this method is called from distant
client calls
'''
return call_command(*args, **kwargs)
2) The other app is a client of that server:
from somewhere import SingletonClass, Client
class Command(base.BaseCommand):
def handle(self, *args, **options):
if options["local"] = True: # wil be True when called from the main app
return SingletonClass().date
else:
client = Client()
options["local"] = True
return client.doSomething(*args, **options)
This can work but I have a manually serialize (on the client side) & deserialize (on the server side) each command. I find it ugly and I wonder if there is a best way to use Django.
I could also use the database to store every single object I want to share but it doesn't seem better.
Real use case below:
For example, I have an app called "Configuration" which is able to load properties files on the file system. (note: I call property file like presented here: docs.python.org/2/library/configparser.html). This app loads the properties files when the user run the command "load-config". Then, I expect all my apps to have access to the configuration previously loaded without having to re-read each property file. I want to instanciate a "PropertyManager" object once for all my apps. So far, I'm storing the properties read in the database so that every app can get it from there
Any comment or ideas?
Upvotes: 1
Views: 267