469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

'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
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

Jan 8 '08 #1
2 1244
Hi,

On Jan 8, 2008 7:24 AM, Tobiah <to**@tobiah.orgwrote:
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
Jan 9 '08 #2

-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of Tobiah
Sent: Monday, January 07, 2008 5:24 PM
To: py*********@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()
Jan 9 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Mars | last post: by
2 posts views Thread by Rajarshi Guha | last post: by
15 posts views Thread by Jay | last post: by
6 posts views Thread by Steven D'Aprano | last post: by
2 posts views Thread by Tumurbaatar S. | last post: by
2 posts views Thread by tobiah | last post: by
10 posts views Thread by =?iso-8859-1?B?QW5kcuk=?= | last post: by
5 posts views Thread by Lie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.