[Zope-dev] ThreadSafeCounter 0.0.1 released

Morten W. Petersen morten@esol.no
12 Feb 2001 01:31:04 +0100


[Chris Withers]

| So would a counter such as:
| 
| class PersistentCounter(Persistent):
| 
|     # create the counter
|     def __init__(self, value=0):
|         self._value = value
| 
|     # get the value of the counter without incrementing
|     def getValue(self):
|         return self._value
| 
|     # increment the counter when called, and return the new value
|     def __call__(self, number=1):
|         self._value = self._value + number
|         return self._value
| 
| ...not be thread safe?

I'm not sure whether the code above is thread safe or not.  ThreadSafeCounter was
made because

	I don't know enough about Zope internals to determine whether
	or not the counter would return unique values, nor do I
	know enough about threads to be completely sure that it
	would, every time.

	File locking is a simple concept, and easy to understand.

As I've understood it, two threads serving requests have a copy each of the
database, and only when changes are committed are they reflected in
the database.  Therefore, two requests created at the same time could
get an identical copy and therefore and identical value.

..so, it basically boils down to the fact that it's simple to use file locking.

Cheers,

Morten