Why do things get started twice in django sometimes? I see the framework being started twice here. Also if you overload the __init__(self): of models, that too gets called twice. I've never understood this, but the singleton pattern seems to provide a nice workaround.
Brian On Mon, Dec 13, 2010 at 5:19 PM, Mike Dewhirst <mi...@dewhirst.com.au>wrote: > On Dec 13, 10:57 am, martvefun <martve...@gmail.com> wrote: > > On 10-12-10 11:09, Mike Dewhirst wrote: > > > On 10/12/2010 7:43pm, martvefun wrote: > > >> On 09-12-10 01:37, Mike Dewhirst wrote: > > >>> It seems like a good place to put it. Maybe you can test to see if > the > > >>> threads have been started already? > > <snip> > > > Thank you, it works if I have to call in the same files (like s1 = > > singleton() ; s2 = singleton() ) but here I still have two calls > > > > class singleton(object): > > """ designed by Oren Tirosh and Jeff Pitman """ > > def __new__(self, *args, **kwargs): > > print "call singleton" > > if not '_singleton' in self.__dict__: > > print "first time created" > > slate = object.__new__(self) > > slate.state = { > > 'threads':True, > > # add other state things as required > > } > > self._singleton = slate > > return self._singleton > > > > singleton() > > print "Server is now runing" > > > > gives me : > > $ python manage.py runserver > > call singleton > > first time created > > Server is now runing > > call singleton > > first time created > > Server is now runing > > Validating models... > > 0 errors found > > I just tested it myself and while it definitely works - meaning it > does return exactly the same object each time - manage.py runserver > apparently starts the framework twice. Hence you see it twice. I > noticed that when you edit a file and the dev server automatically > restarts, the singleton gets recreated but only once as expected. > > Here is some research by Graham Dumpleton earlier this year and which > explains the process in detail ... > > http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html > > If you decide to use a singleton to monitor state you might have to > look for somewhere safe to start it. > > > > > And anyway I just realised another problem : I'll need to have access to > > the objects I created from views. > > I think it would work from anywhere you can call the code but you need > to test it. > > Mike > > > The idea is to send a message to the thread when a certain action is > > done (like user registration, validate choices...) but to do that I need > > an access to this object (I cannot create another one because of > > concurrency safety) > > > > Any idea ? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- Brian Bouterse ITng Services -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.