[Grok-dev] Re: Managing non-persisted objects

Laurence Rowe l at lrowe.co.uk
Mon Jun 30 10:56:39 EDT 2008


Leonardo Rochael Almeida wrote:
> Another aproach is to create one COM object per thread and store it in
> a module-level global dictionary keyed by the thread id. Then in your
> persistent class you create a method or property to do a "fetch or
> create" on this dictionary.
> 
> I'd go as far as saying you don't even need to worry about locking
> this module-level global dictionary before querying or modifying it
> since you'd never have a race condition on the presence of the
> "thread-id" keyed value, and the Python GIL would take care of the
> contention on the dictionary itself.

Another approach is to use _v_ attributes on a persistent object, you 
get one per thread:

@property
def myapp(self):
     app = getattr(self, '_v_myapp', None)
     if app is None
         self._v_myapp = app = NewApp()
     return app

threading.local also provides a nice wrapper around the whole keying by 
thread id.

You may be better off with one instance per `session` though. Depends on 
whether you need to preserve state in the app between requests.

Laurence



More information about the Grok-dev mailing list