Re: synchronized method

From: Max Ischenko (max_at_ucmg.com.ua.remove.it)
Date: 02/10/04


Date: Tue, 10 Feb 2004 14:54:32 +0200

Graham Dumpleton wrote:

>>I wrote simple implementation of the "synchronized" methods (a-la Java),
>>could you please check if it is OK:
>>
>>def synchronized(method):
>>
>> """
>> Guards method execution, similar to Java's synchronized keyword.
>>
>> The class which uses this method, is required to have a
>> L{threading.Lock} primitive as an attribute named 'lock'.
>> """
>>
>> def wrapper(self, *args):
>> self.lock.acquire()
>> try:
>> return method(self, *args)
>> finally:
>> self.lock.release()
>> return wrapper
>
>
> Under normal execution the lock will not be released because you return
> from the try clause.

Hmm. I think you must be wrong:

>>> def foo():
        try:
                return 1
        finally:
                print 'ok'

                
>>> foo()
ok
1

> There is also the case that method isn't defined.

Indeed, thanks for pointing this out.

> Someone else was asking about this sort of thing back the end of January.
> The post is included below. As much as doing this sort of thing may sound
> like a good way of handling synchronisation, it still has its problems.

[ cut ]

I'll look into it.



Relevant Pages

  • Re: synchronized method
    ... > Guards method execution, similar to Java's synchronized keyword. ... Under normal execution the lock will not be released because you return ... The calling thread may acquire the lock multiple times, ...
    (comp.lang.python)
  • Re: synchronized method
    ... Graham Dumpleton wrote: ... >> Guards method execution, similar to Java's synchronized keyword. ... the finally clause would be pretty useless in many ...
    (comp.lang.python)
  • Re: synchronized statements
    ... > A quick solution not involving the synchronized keyword is to print only one ... This doesn't stop the printing of one set of strings interrupting the ... I'm surprised acquiring the lock on System.out didn't work. ...
    (comp.lang.java)
  • Re: Synchronization constructs in Java and their implications
    ... one lock using the codes posted. ... synchronized keyword was the slowest one: ... Prev by Date: ...
    (comp.programming.threads)
  • Re: synchronized method
    ... Max Ischenko wrote: ... > def synchronized: ... > Guards method execution, similar to Java's synchronized keyword. ...
    (comp.lang.python)