Synchronization mixin in Python anyone?
Chris Liechti
cliechti at gmx.net
Mon May 6 13:30:18 EDT 2002
Gustavo Cordova <gcordova at hebmex.com> wrote in
news:mailman.1020692951.25653.python-list at python.org:
>> Gustavo Cordova <gcordova at hebmex.com> wrote in
>> news:mailman.1020461587.9261.python-list at python.org:
>> try:
>> args = self._args + args
>> kw.update(self._kw)
>> return = self._function(*args, **kw)
>> finally:
>> self._lock.release()
>>
>
> But what about any exception that's raised?
>
> Does the finally: clause re-raise the exception so that
> it can be caught upstream?
yes, as Alex already told.
the neat thing about try: finaly: is that it is realy executed in any case,
no matter what you do in there from "break" over "raise" to "return".
>> the SynchronizedCall wrapper might be too simple for OO. i
>> think it makes more sense when one method can lock out another
>> on the same instance, like "synchronized" in Java (e.g. get
>> and set methods for an attribute can not be called at the same
>> time). maybe it is that what the OP wanted with his mix-in.
>
> Yes, I was thinking of something like that with the regex example,
> a way to block between the time a thread enters *any* method of an
> instance, until it exits the method. And that's where it gets
> complicated.
>
> Maybe with some little hacking of the interpreter, we could make
> a "synchronized" function, like "property()".
>
> Who knows :-)
i do :)) well after i answered to you post i had to play around with that
for myself. i've posted a "synchronized" function that you can use like
"staticmethod". i've posted in a separate message in this thread, but i can
send it again if you're interested.
it has only a small issue. the first time a synchronized method is called
is critical - only _one_ thread must do this or more than one lock could be
created and all but one are lost. but this can easily be solved by making
__init__ synchronized. this does not harm and is guaranteed to be only
called once per instace by a single thread.
i don't want that _all_ method get "synchronized" for an instance - just
selected ones. the reason is that sometimes you need async messages. e.g.
if that object holds a flag which you want to set but you don't want to
wait because an other thread uses a method.
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list