how to write thread-safe module ? and pytz
daniel.dittmar at sap.corp
Wed Aug 10 15:46:45 CEST 2005
> Does someone know if the module pytz
> (http://sourceforge.net/projects/pytz/) is thread-safe ?
> I have not seen it explicitely stated, and just wanted to be sure, as I
> want to use it.
> That's because in the file pytz/tzinfo.py, I see global variables
> _timedelta_cache, _datetime_cache, _ttinfo_cache, which are
> dictionnaries and are used as cache.
> I always thought that you must protect shared data with locks when
> multithreading, but I don't see any lock anywhere in pytz.
Dictionaries (and probably most other Python types that are implemented
in C) are inherently thread safe.
This applies only to the individual methods of dictionaries. The
following code would still require a lock:
if mydict.has_key (keyval):
variable = mydict [keyval]
because a second thread could delete the entry between the calls to
has_key and __getvalue__.
mydict [keyval] = mydict.get (keyval, 0) + 1
is also an candidate for problems.
> However, pytz seems to work well with multiple threads creating various
> timezone objects at the same time.
'Seems to work' is never a good argument with regard to threads.
Especially if you're testing on a single CPU machine.
More information about the Python-list