473,385 Members | 1,766 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,385 software developers and data experts.

Local class variables? (mod_python problem)

We have a set of classes using static methods to retain reference
variables between operations. The problem is that the static variables
are not reset between operations when used through mod_python.

Although it is possible to reset the class variables between invocations
of the system, this has the potential of 'wiping out' these variables
when another user is using the system.

Is there a way of getting the equivalent of 'local class variables'? In
other words, a way of making 'print a' and 'print b' below provide the
same output?

Regards
Rory
class TryMe(object):
x = 0
y = 0

def __init__(self):
self.a = 0, self.b = 0

@staticmethod
def incrementer():
TryMe.x += 1, TryMe.y += 1

def addone (self):
TryMe.x += 1, TryMe.y += 1
self.a , += 1 self.b += 1

def __repr__(self):
return """
TryMe.x = %d TryMe.y = %d self.a = %d self.b = %d
""" % (TryMe.x, TryMe.y, self.a, self.b)

if __name__ == '__main__':

a = TryMe()
a.incrementer()
a.addone()

b = TryMe()
b.incrementer()
b.addone()

print 'a:', a
print 'b:', b
--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Feb 22 '07 #1
9 2198
>>>>Rory Campbell-Lange <ro**@campbell-lange.net(RC) wrote:
>RCWe have a set of classes using static methods to retain reference
RCvariables between operations. The problem is that the static variables
RCare not reset between operations when used through mod_python.
>RCAlthough it is possible to reset the class variables between invocations
RCof the system, this has the potential of 'wiping out' these variables
RCwhen another user is using the system.
>RCIs there a way of getting the equivalent of 'local class variables'? In
RCother words, a way of making 'print a' and 'print b' below provide the
RCsame output?
There are several errors in your python code: quite a number of comma's
have to be replaced by semicolons (or newlines), and there is a spurious
comma.

And a and b already print out the same, so it is not clear what you want.
Do you mean that the x and y of the a and b object should be independent?
In that case you should not use static variables but instance variables.
If they have to survive across different HTTP requests you should use
sessions.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Feb 22 '07 #2
Rory Campbell-Lange wrote:
We have a set of classes using static methods to retain reference
variables between operations. The problem is that the static variables
are not reset between operations when used through mod_python.

Although it is possible to reset the class variables between invocations
of the system, this has the potential of 'wiping out' these variables
when another user is using the system.

Is there a way of getting the equivalent of 'local class variables'? In
other words, a way of making 'print a' and 'print b' below provide the
same output?

It's very unclear what you mean here, and I'm additionally under the
impression that you are deep in the murky waters of accidential concurrent
access errors here.

I suggest you explain better what these variables are supposed to contain,
for whom, and for how long, and then we might suggest a better solution.

Diez
Feb 22 '07 #3
Apologies to Piet and Diez for the lack of clarity in my previous post
(and the broken code).

In essence we use class variables as follows:

class Part (object):
totalgia = 0
def __init__(self, gia):
self.gia = gia # gross internal area
self.giaratio = 0
Part.totalgia += self.gia
def addavgbm(self):
self.giaratio = float(self.gia)/float(Part.totalgia)
def __repr__(self):
return "gia: %0.1f giaratio: %0.2f" % (self.gia, self.giaratio)

if __name__ == '__main__':
p1 = Part(20)
p2 = Part(30)
for p in p1, p2:
p.addavgbm()
print p

totalgia keeps incrementing when this code is used under mod_python.

We most certainly are in 'murky waters of accidental concurrent access'.
A life vest would be gratefully received.

Kind regards
Rory
On 22/02/07, Rory Campbell-Lange (ro**@campbell-lange.net) wrote:
We have a set of classes using static methods to retain reference
variables between operations. The problem is that the static variables
are not reset between operations when used through mod_python.

Although it is possible to reset the class variables between invocations
of the system, this has the potential of 'wiping out' these variables
when another user is using the system.

Is there a way of getting the equivalent of 'local class variables'? In
other words, a way of making 'print a' and 'print b' below provide the
same output?
On 22/02/07, Piet van Oostrum (pi**@cs.uu.nl) wrote:
>>>Rory Campbell-Lange <ro**@campbell-lange.net(RC) wrote:
There are several errors in your python code: quite a number of comma's
have to be replaced by semicolons (or newlines), and there is a spurious
comma.

On 22/02/07, Diez B. Roggisch (de***@nospam.web.de) wrote:
Rory Campbell-Lange wrote:
It's very unclear what you mean here, and I'm additionally under the
impression that you are deep in the murky waters of accidential
concurrent access errors here.

--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Feb 22 '07 #4
>>>>Rory Campbell-Lange <ro**@campbell-lange.net(RC) wrote:
>RCtotalgia keeps incrementing when this code is used under mod_python.
And also when used outside of mod_python. It is because it is a class level
variable. In fact I think under certain circumstances in mod_python it will
not do that because different requests can run in different Apache
processes (on Linux, Unix, Mac OS X etc.). So it this desired behaviour or
not? Your post isn't clear about that. And if it isn't what is the desired
behaviour?

And you certainly should do something about the concurrent access.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Feb 22 '07 #5
On 22/02/07, Rory Campbell-Lange (ro**@campbell-lange.net) wrote:
In essence we use class variables as follows:

class Part (object):
totalgia = 0
def __init__(self, gia):
self.gia = gia # gross internal area
self.giaratio = 0
Part.totalgia += self.gia
def addavgbm(self):
self.giaratio = float(self.gia)/float(Part.totalgia)
def __repr__(self):
return "gia: %0.1f giaratio: %0.2f" % (self.gia, self.giaratio)

if __name__ == '__main__':
p1 = Part(20)
p2 = Part(30)
for p in p1, p2:
p.addavgbm()
print p

totalgia keeps incrementing when this code is used under mod_python.
On 22/02/07, Rory Campbell-Lange (ro**@campbell-lange.net) wrote:
On 22/02/07, Piet van Oostrum (pi**@cs.uu.nl) wrote:
>>>Rory Campbell-Lange <ro**@campbell-lange.net(RC) wrote:
RCtotalgia keeps incrementing when this code is used under mod_python.

And also when used outside of mod_python. It is because it is a class level
variable. In fact I think under certain circumstances in mod_python it will
not do that because different requests can run in different Apache
processes (on Linux, Unix, Mac OS X etc.). So it this desired behaviour or
not? Your post isn't clear about that. And if it isn't what is the desired
behaviour?

And you certainly should do something about the concurrent access.
It is not desirable for the class variable to keep incrementing outside
of invocations of '__main__', as is the case when it is loaded under
mod_python under apache2 on linux.

I would be grateful for pointers on dealing with concurrent access.

Regards
Rory
--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Feb 22 '07 #6
Rory Campbell-Lange wrote:
class Part (object):
totalgia = 0
def __init__(self, gia):
self.gia = gia # gross internal area
self.giaratio = 0
Part.totalgia += self.gia

if __name__ == '__main__':
p1 = Part(20)
p2 = Part(30)
for p in p1, p2:
p.addavgbm()
print p
You need another class, such as PartGroup, to keep track of
the totalgia of a group of parts (as an instance variable, not
a class variable). Then you can create a new instance of it
in your __main__ code, e.g.

class PartGroup(object):

def __init__(self):
self.parts = []
self.totalgia = 0

class Part(object):

def __init__(self, group, gia):
self.gia = gia
group.parts.append(self)
group.gia += gia

if __name__ == "__main__":
parts = PartGroup()
p1 = Part(parts, 10)
p2 = Part(parts, 20)
print parts.totalgia

A possible variation would be not to store the totalgia at
all, but have a function or method that calculates it from
the list of parts when you need it. Whether that's better or
not will depend on how frequently you need the value.

--
Greg
>
totalgia keeps incrementing when this code is used under mod_python.

We most certainly are in 'murky waters of accidental concurrent access'.
A life vest would be gratefully received.

Kind regards
Rory
On 22/02/07, Rory Campbell-Lange (ro**@campbell-lange.net) wrote:
>>We have a set of classes using static methods to retain reference
variables between operations. The problem is that the static variables
are not reset between operations when used through mod_python.

Although it is possible to reset the class variables between invocations
of the system, this has the potential of 'wiping out' these variables
when another user is using the system.

Is there a way of getting the equivalent of 'local class variables'? In
other words, a way of making 'print a' and 'print b' below provide the
same output?


On 22/02/07, Piet van Oostrum (pi**@cs.uu.nl) wrote:
>>>>>>>Rory Campbell-Lange <ro**@campbell-lange.net(RC) wrote:

>>There are several errors in your python code: quite a number of comma's
have to be replaced by semicolons (or newlines), and there is a spurious
comma.

On 22/02/07, Diez B. Roggisch (de***@nospam.web.de) wrote:
>>Rory Campbell-Lange wrote:

>>It's very unclear what you mean here, and I'm additionally under the
impression that you are deep in the murky waters of accidential
concurrent access errors here.


Feb 22 '07 #7
It is not desirable for the class variable to keep incrementing outside
of invocations of '__main__', as is the case when it is loaded under
mod_python under apache2 on linux.
I'm still not clear on what you want to accomplish. In the end it boils down
to who is supposed to share that information in the variables, or in other
words: which scope has it.

Is it per request? Then using some thread-local storage would be in order,
or "abusing" a possible request-object.

Is it per user, over several requests? Then you need a session-mechanism.

Is it per application, for several users, over several requests? Then your
approach is ok, but needs guarding against concurrrent access using
threading.Lock for example. However, I presume that is not the desired
usecase, from what I can extract from your posts I presume it's case two.

Diez
Feb 23 '07 #8
On 23/02/07, Diez B. Roggisch (de***@nospam.web.de) wrote:
It is not desirable for the class variable to keep incrementing outside
of invocations of '__main__', as is the case when it is loaded under
mod_python under apache2 on linux.

I'm still not clear on what you want to accomplish. In the end it boils down
to who is supposed to share that information in the variables, or in other
words: which scope has it.

Is it per request? Then using some thread-local storage would be in order,
or "abusing" a possible request-object.

Is it per user, over several requests? Then you need a session-mechanism.

Is it per application, for several users, over several requests? Then your
approach is ok, but needs guarding against concurrrent access using
threading.Lock for example. However, I presume that is not the desired
usecase, from what I can extract from your posts I presume it's case two.
Many thanks for your reply. The use case is per request, and I would be
grateful to learn more about thread-local storage.

Kind regards
Rory

--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Feb 23 '07 #9
>
Many thanks for your reply. The use case is per request, and I would be
grateful to learn more about thread-local storage.
There are several ways. I'm not familiar with mod_python, but I guess you
get a dict-like object for the request parameters. In python, this is
usually writable (in contrast to the darn J2EE-spec'ed HttpRequest)

So just add a new key, e.g. "working_state", to it. Under that, file those
variables.

Alternatively, there are recipes out there
(http://aspn.activestate.com/ASPN/Coo.../Recipe/302088
)

that give you basically a dictionary keyed with the current thread.

That is helpful if you have computations deeper in code that can't get a
hold on the request object. Might be the case in mod_python - in cherrypy,
the request itself is thread-locally stored, so you can get it whereever
you need.

Diez
Feb 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wolf | last post by:
i would like to briefly share my experiences with installing mod_python on a w2000 box. i must say that i believe the installation process to be unnecessarily complicated by the simple fact that...
0
by: Anthony Raj | last post by:
There's a problem which I'm trying to solve as elegently as possible. Its basically a setup that runs mod_python on Apache and its for a client that needs to generate dynamic reports by pulling...
1
by: py.adriano | last post by:
Hi folks... I'm getting a weird problem while loading psp module from mod_python. It only happen somethimes, and I just can't figure out why. I saw other threads in other lists over the net, but...
1
by: neha | last post by:
hi, i m trying to integrate python with apache on linux.For this i m using mod_python. I dont see any problem with the versions of python,apache and mod_python i m using. the versions i m using...
6
by: Anthony L. | last post by:
I am writing a web application that is comparable to a content management system used in blogging. I really want to use Python after having done some evaluation coding using Python 2.3.5 with...
2
by: exhuma.twn | last post by:
Hi again, as soon as I try to make use of the "session" object inside a psp-template file, I get the following error: Mod_python error: "PythonHandler mod_python.publisher" Traceback (most...
5
by: m.banaouas | last post by:
Hi, bonjour, witch versions are suitable to use for apache & mod_python ? Can i install and use "Apache 2.2.3" & "mod_python 3.2.10" (most recent versions) without facing any known major...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
0
by: Formula | last post by:
Hello everybody,because I am newbie in python two weeks only but I had programming in another languages but the python take my heart there's 3 kind of arrays Wow now I hate JAVA :) . I am working...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.