473,387 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

'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 1364
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mars | last post by:
I have looked long and hard at Mr. Martelli's Borg recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 It is a very useful substitute for a Singleton, but I can't figure out...
2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
6
by: Steven D'Aprano | last post by:
I've been working with the Borg design pattern from here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 and I'm having problems subclassing it. I'm a newbie, so I've probably...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
2
by: tobiah | last post by:
I am making a web app, made up of many modules that all need access to some important data, like the current session data, cookies, navigation history, post/get variables, etc. I decided to go...
10
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
In my application, I make use of the Borg idiom, invented by Alex Martelli. class Borg(object): '''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273 Derive a class form this; all...
1
by: seanacais | last post by:
I want to create a class derived from a Borg class that can instantiated as part of a script or be contained in other classes. When methods from the Borg class are called, I would like to know the...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.