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?
>>>
>>> Here is a singleton which could live in your __init__.py and might
>>> help to record the state of your threading ... or anything else for
>>> that matter.
>>>
>>> class singleton(object):
>>> """ designed by Oren Tirosh and Jeff Pitman """
>>> def __new__(self, *args, **kwargs):
>>> if not '_singleton' in self.__dict__:
>>> slate = object.__new__(self)
>>> slate.state = {
>>> 'threads':True,
>>> # add other state things as required
>>> }
>>> self._singleton = slate
>>> return self._singleton
>>>
>>> hth
>>>
>>> Mike
>>>
>> Sorry but I don't really understand how works the function you gave me.
>
> A singleton is a class which guarantees to return exactly the same
> object every time. It can only create an object once then returns a
> handle to the existing object instead of creating a new one.
>
> If you initiate your threads and set (in the above example)
> slate.state['threads'] = True I think you can rely on that and avoid
> initiating them twice.
>
> I used it once to manage the state of a "switch" where different parts
> of the project could instantiate a singleton object and read the
> state, make decisions and update the state reliably for other part of
> the app.
>
> Mike
>
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
And anyway I just realised another problem : I'll need to have access to
the objects I created from views.
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.