Local class variables? (mod_python problem)
Rory Campbell-Lange
rory at campbell-lange.net
Thu Feb 22 12:17:27 EST 2007
On 22/02/07, Rory Campbell-Lange (rory at 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 (rory at campbell-lange.net) wrote:
On 22/02/07, Piet van Oostrum (piet at cs.uu.nl) wrote:
> >>>>> Rory Campbell-Lange <rory at campbell-lange.net> (RC) wrote:
> >RC> totalgia 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
<rory at campbell-lange.net>
<www.campbell-lange.net>
More information about the Python-list
mailing list