[Python-ideas] Monitor implementation for the stdlib?

Leonardo Santagada santagada at gmail.com
Mon Oct 22 05:43:22 CEST 2007


Em 21/10/2007, às 23:17, Guido van Rossum escreveu:

> 2007/10/21, Christopher D. Leary <christopher.leary at cornell.edu>:
>> Though I do think that the with statement is an excellent addition to
>> the language (it avoids much of the ugliness and chance for error in
>> symmetric P and V), I don't believe that it adequately covers the use
>> case that monitors are intended for; namely, encapsulation and
>> abstraction of the mutual exclusion involved in locking around / 
>> several
>> data entities/ with /several defined functionalities/.
>
> Locks can do that too of course.

Of course, as you know his code is writen using locks

> It looks like a monitor is similar to 'synchronized' in Java except it
> appears the default (once inheriting from Monitor) is synchronized,
> and you need to use a decorator to declare an unsynchronized method.
> Have I got that right?

Yep, any java class with synchronized is a monitor.

> It seems to be a matter of granularity. With monitors, one is required
> to create a new class for each group of variables that require
> synchronization. With locks, that is not necessary. Which is better
> depends on the complexity of the group of variables and (especially)
> the operations.
>
> A more "Pythonic" approach would be to implement an @synchronized
> decorator; Python users are more likely to be familiar with Java terms
> than with classic Hoare or P and V.
>
> (I'm sure someone has already implemented @synchronized, but it hasn't
> made it into the standard library -- perhaps an indication that its
> importance is more theoretical than practical, perhaps simply due to a
> time lag.)

There is some implementations of @synchronized on the python cookbook  
at ASPN, but  them don't implement monitors, just a way to make a non- 
reentrant method.

I think Christopher implemented monitors that by default have all its  
methods as if they where java synchronized is because that is most  
common in python where you don't have the abundance of geters and  
static methods that java has.

I would like to see monitors in the python std library if for no  
other reason to let concurrency teachers and java fanatics happy. I'm  
(althought still sitting on my hands about it) wanting to see STM on  
python.

--
Leonardo Santagada




More information about the Python-ideas mailing list