1 """
2 from Thinking in Python, Bruce Eckel
3 http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Observer.html
4
5 (c) Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
6
7 Simple emulation of Java's 'synchronized'
8 keyword, from Peter Norvig.
9 """
10
11 from threading import RLock
12
13
15
16 def f(*args):
17 self = args[0]
18 self.mutex.acquire()
19
20 try:
21 return method(*args)
22 finally:
23 self.mutex.release()
24
25 return f
26
27
29 """Synchronize methods in the given class.
30 Only synchronize the methods whose names are
31 given, or all methods if names=None."""
32 if type(names) == type(''):
33 names = names.split()
34 for (name, val) in list(klass.__dict__.items()):
35 if callable(val) and name != '__init__' and \
36 (names == None or name in names):
37
38 setattr(klass, name, synchronized(val))
39
40
46