1 """
2 from Thinking in Python, Bruce Eckel
3 http://mindview.net/Books/TIPython
4
5 Simple emulation of Java's 'synchronized'
6 keyword, from Peter Norvig.
7 """
8
9 from threading import RLock
10
11
13
14 def f(*args):
15 self = args[0]
16 self.mutex.acquire()
17
18 try:
19 return apply(method, args)
20 finally:
21 self.mutex.release()
22
23 return f
24
25
27 """Synchronize methods in the given class.
28 Only synchronize the methods whose names are
29 given, or all methods if names=None."""
30 if type(names) == type(''):
31 names = names.split()
32 for (name, val) in klass.__dict__.items():
33 if callable(val) and name != '__init__' and \
34 (names == None or name in names):
35
36 klass.__dict__[name] = synchronized(val)
37
38
44