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