Hello ! I have been created a Zope (Python) Product. But it is must store inner global definitions (some conversion parameters), what I need to change in only one thread, or only once. See below. So: because the Zope is threaded application, the threads are use this product. Possible in same time. Question: the python variable setting/checking is thread-safe ? In Delphi, or C, with threaded application I must acquire/release a lock, when I check/set/read any variable (except integer), like this (pseudo): lock=Lock func ReadVar():string; lock.Enter; try Result:=GlobalVar; finally lock.Leave; end; proc WriteVar(val:string); lock.Enter; try GlobalVar:=val; finally lock.Leave; end; This way is make the variable using thread-safe, because avoid to thread A read while thread B is write (and subsections of the information are confusing by another thread). My code in python: A. version) def __init__(self): self.Fmts=[['date','%m/%d/%Y'],['time','%H:%M.%S']] # default is for hungarian def SetConversionFormats(self,Fmts): s1=str(Fmts) s2=str(self.Fmts) if (s1<>s2)): self.Fmts=Fmts B. version) def __init__(self): self.DefFmts=[['date','%Y.%m.%d'],['time','%H:%M.%S']] # default is for hungarian self.Fmts=None def SetConversionFormats(self,Fmts): if self.Fmts==None: self.Fmts=Fmts Usage: updsqlobj=context.updsqlobj() updsqlobj.SetConversionFormats([['date','%Y.%m.%d'],['time','%H:%M.%S']],True) updsqlobj.GenerateUpdateSQL(..) updsqlobj.Apply(..) The Fmts is a list. I want to set the parameters of Product in only once, but because the Zope is threaded app, I must thinking in thread-safe programming methods. Question1: The code above is thread safe ? (I think, it is not) Question2: If it is not thread safe, how to set this variable without global lock/mutex object ? Question3: Or how to I set this object's parameters as property, in Designer interface of Zope ? Please help me, if possible ! Thanx for it, and for every advance: -- Best regards, fowlertrainer mailto:fowlertrainer@anonym.hu
fowlertrainer@anonym.hu wrote:
Question: the python variable setting/checking is thread-safe ?
Python has Lock objects just like Delphi and C, I'd wrap your setting/checking code in the same way as you would for Delphi/C...
I want to set the parameters of Product in only once, but because the Zope is threaded app, I must thinking in thread-safe programming methods.
Indeed, Zope and ZODb usually take care of all of this for you, but you're doing something out of the ordinary, so need to worry aboutit ...
Question1: The code above is thread safe ? (I think, it is not)
No.
Question2: If it is not thread safe, how to set this variable without global lock/mutex object ?
You need to put a lock around those bits of code.
Question3: Or how to I set this object's parameters as property, in Designer interface of Zope ?
I'd just have them as Zope Properties on your container object, then you don't need to worry about the threading. context.manage_addProperty('prop name','value','string') ...for example. cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (2)
-
Chris Withers -
fowlertrainerļ¼ anonym.hu