On August 19, 2002 03:45 pm, Matt Feifarek wrote:
> | Well, most of the time we just put persistent values in module-level
> | globals (remembering to do proper locking via threading.Lock, and often
> | using dictionaries to organize the data).
>
> Can you elaborate on that threading.lock bit a little? We use a similar
> technique, and don't mess with threading.

If two or more threads attempt to set/read a shared data value, such as a 
module global, at the same time you can run into synchronization issues.  
threading.Lock can be used to avoid them.

I think it's best to use use Locks in combination with a singleton or borg 
class rather than with module globals:

from threading import Lock
class Counter:
   _count = 1
   __sharedState = {'_lock': Lock()}
   def __init__(self):
        self.__dict__ = self.__sharedState

   def count(self):
       return self._count

   def increment(self):
       self._lock.acquire()
       self._lock += 1
       self._lock.release()
 
   def decrement(self):
       self._lock.acquire()
       self._lock -= 1
       self._lock.release()



    


-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to