Package smartcard :: Module Synchronization
[hide private]
[frames] | no frames]

Source Code for Module smartcard.Synchronization

 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   
12 -def synchronized(method):
13 14 def f(*args): 15 self = args[0] 16 self.mutex.acquire() 17 # print method.__name__, 'acquired' 18 try: 19 return apply(method, args) 20 finally: 21 self.mutex.release()
22 # print method.__name__, 'released' 23 return f 24 25
26 -def synchronize(klass, names=None):
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 # print "synchronizing", name 36 klass.__dict__[name] = synchronized(val)
37 38
39 -class Synchronization:
40 # You can create your own self.mutex, or inherit from this class: 41
42 - def __init__(self):
43 self.mutex = RLock()
44