On Thursday December 06, 2001 07:15 pm, Daniel Popowich wrote:
> > >    3. Use instance attributes for data meaningful across all
> > > sessions and requests.
> >
> > No, use something else for this.  You should use a database, a ZODB
> > store, or something else with the appropriate synchronization
> > mechanisms to avoid conflicts between threads.  Store a reference to
> > your shared object in a python module that can be imported from
> > anywhere, or as an attibute of the Application.
>
> I'm thinking about data like that found in the Colors example that
> comes with WebWare.  It's painful to compute everytime and since the
> data will not change across all users/sessions/requests, caching it in
> the instance is fine.

Caching it in the instance will work, but that means you'll be computing the 
same thing once per copy of the servlet -- if you have 10 threads, you'll be 
computing the same thing up to 10 times.  It's more efficient to store the 
data in either the class, or as a global variable.  For example, here's how 
you can store it in the class (untested, but I've used code like this before):

class MyPage(Page):
        # These are class variables, so there's only one copy of these
        _dataLock = threading.Lock()
        _data = None

        # Call this whenever you need the data that computeData
        # returns, but you only want it computed once.
        def data(self):
                # Check if we haven't yet computed _data
                if MyPage._data is None:
                        # If not, use a thread lock to ensure that
                        # only one thread actually computes it
                        MyPage._dataLock.acquire()
                        try:
                                if MyPage._data is None:
                                        MyPage._data = computeData()
                        finally:
                                MyPage._dataLock.release()
                return MyPage._data


- Geoff

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to