how to write thread-safe module ? and pytz

Bryan Olson fakeaddress at
Wed Aug 10 15:46:38 CEST 2005

nicolas_riesch wrote:
 > Does someone know if the module pytz
 > ( is thread-safe ?

On that, I don't know.

 > That's because in the file pytz/, 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.

Definitely stick with what you always thought.

 > However, pytz seems to work well with multiple threads creating various
 > timezone objects at the same time.

Buggy threading can seem to work well for along time. It may be
a billion-to-one shot that a thread switch happens at just the
wrong time.

 > I don't understand where the trick is, that allows multiple threads to
 > access this module without any locking and that all this seems to work
 > without any problem...
 > Does this mean that there is a means to write a module that is
 > thread-safe, without importing the threading module and creating locks
 > ?
 > Or have I not understood something ...?
 > Can someone give me a hint ?

In the current Python implementation, more things are atomic
than the language guarantees to be atomic. Programmers should
not depend on that behavior. Again, I don't know anything about
pytz, but we wouldn't bother with locks and semaphores and such
if we could make the problems go away just by ignoring them.


More information about the Python-list mailing list