[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

'Borg' and multiple threads.

Tobiah

1/7/2008 10:24:00 PM

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.te...

2 Answers

Mike Mazur

1/9/2008 1:17:00 AM

0

Hi,

On Jan 8, 2008 7:24 AM, Tobiah <toby@tobiah.org> wrote:
> 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?

Maybe a silly question, but are you changing the values in the dict
before hitting it again and getting the empty dict?

Mike

Reedick, Andrew

1/9/2008 3:02:00 PM

0



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Tobiah
> Sent: Monday, January 07, 2008 5:24 PM
> To: python-list@python.org
> Subject: 'Borg' and multiple threads.
>
> 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
>
>
>
> 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?
>


I had a similar question except mine was from a bug. (Thinking in c++
lead me to inadvertently create static class vars.)

You can "print self" and use the id() function to get the memory
addresses of the variables and see what's going on. In the code below,
mem4 is equivalent to your static_state.

count = 1

class Foo:

mem4 = {}
mem5 = {}

def __init__(self):
self.mem = {}

global count
count += 1
self.mem2 = count

self.mem3 = "%x" % (id(self))

self.mem5 = {}

print 'init count =', count


def me(self):
print "object: ", self
print "\tid(self.mem) %x" % (id(self.mem)), " self.mem
=", self.mem
print "\tid(self.mem2) %x" % (id(self.mem2)), "
self.mem2 =", self.mem2
print "\tid(self.mem3) %x" % (id(self.mem3)), "
self.mem3 =", self.mem3
print "\tid(self.mem4) %x" % (id(self.mem4)), "
self.mem4 =", self.mem4
print "\tid(self.mem5) %x" % (id(self.mem5)), "
self.mem5 =", self.mem5
global count
count += 1
print '\tcount =', count
self.mem4[count] = count
print "\tid(self.mem4) %x" % (id(self.mem4)), "
self.mem4 =", self.mem4
#self.mem += count
#print "\tid(self.mem) %x" % (id(self.mem)), " self.mem
=", self.mem
print


a = Foo()
b = Foo()
c = Foo()

a.me()
b.me()
c.me()