Synchronization mixin in Python anyone?
Sean McGrath
sean.mcgrath at propylon.com
Sat May 4 04:17:42 EDT 2002
Gustavo,
Thanks. That is helpful.
regards,
Sean
[Sean McGrath]
>> The Zope system contains a Synchronization mixin class
>> for thread synchronization written in C. Derive a class from
>> Synchronized
>> and any mythod calls on an instance of the class are
>> thread safe.
>>
>> Does anybody know of any Python implementation? The C version
>> exhibits "interesting" refcounting behavior I need to work around.
[Gustavo Cordova]
>Hmmm... interesting problem. I've been playing with a function
>synchronization class lately, just toying with the idea. I don't
>know if anybody wants it, but here's the basic skeleton:
>### SynchronizedCall
>import thread
>class SynchronizedCall:
> def __init__(self, function, *args, **kw):
> self._lock = thread.Lock()
> self._args = args
> self._kw = kw
> self._function = function
> def __call__(self, *args, **kw):
> self._lock.acquire()
> try:
> args = self._args + args
> kw.update(self._kw)
> result = self._function(*args, **kw)
> ## In case of an exception, release lock first.
> except:
> self._lock.release()
> raise
> self._lock.release()
> return result
More information about the Python-list
mailing list