[Zope] Zope + Python thread safety

fowlertrainer at anonym.hu fowlertrainer at anonym.hu
Fri Mar 26 03:07:42 EST 2004


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 at anonym.hu




More information about the Zope mailing list