Monitor class (was: The Python Way)

Zooko zooko at zooko.com
Wed Apr 3 12:03:43 EST 2002


There was discussion about a general lock-manager meta-class:

http://groups.google.com/groups?th=1e465bd53977862f

I think this is what is called a "Monitor".  It's an object which contains a 
lock, and whenever you invoke any method of that object the lock is acquired 
before the method is run and then released afterward.

I implemented something like this a couple of years ago and it has been in 
continuous use ever since: [1].  It comes with unit tests that show what it does
and give some limited assurance that it works [2].

There are several reason why you shouldn't trust this code too much:

1.  I don't use it much anymore as I've moved to an event-based paradigm with 
very little need for locking.  (See [3].)

2.  This Python meta-programming stuff is *funky*.  I tried at least three other 
designs for this purpose which never quite worked right before I finally hit 
upon this one.  Mistakes on my part or changes in new versions of Python might 
introduce subtle errors.

3.  The code has been moved around and massaged a bit and it might have broken 
in some subtle way.

On the other hand, it does pass three unit tests and if you try using it and 
have some problems (including the problem of "I don't understand how to use 
this!"), then I'll fix it for you in the interests of keeping my pyutil 
library [4] will be up to date and useful.

Regards,

Zooko

[1] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/pyutil/pyutil/Monitor.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
[2] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/pyutil/pyutil/Monitor_unittest.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
[3] http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/pyutil/pyutil/DoQ.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
[4] http://sf.net/projects/pyutil

---
                 zooko.com
Security and Distributed Systems Engineering
---





More information about the Python-list mailing list