'Borg' and multiple threads.

T

Tobiah

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
 
M

Mike Mazur

Hi,

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
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of Tobiah
Sent: Monday, January 07, 2008 5:24 PM
To: (e-mail address removed)
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top