'Borg' and multiple threads.

Tobiah toby at tobiah.org
Mon Jan 7 17:24:02 EST 2008


I have a class that I call Borg that starts like this:

class Borg(dict):

        static_state = {}
        def __init__(self):
                self.__dict__ = self.static_state


so that I can access the same data from anywhere within
any module or function just by instantiating one.

This is used in a cherrypy web app.  I got to thinking
about whether there would be confusion when multiple
users are eventually hitting the site at the same time.
Everything seems ok.  Each time I hit the app and examine
the Borg() at the beginning, it seems to have no attributes.
This is what I want.

My question is why this seems to work.  I had the idea that
there was a class object that is created when the file containing
the definition is read, which actually contains the static
information that is later accessed by instances.  Isn't this
done when the cherrypy app first loads, rather than each time
a browser hits the app?  Otherwise, where is the individual data
stored for each of two simultaneous hits of the web page?

Thanks,

Tobiah

-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list