import threading class SynchoSectionTimeout(Exception): def __init__(self, message, errors): super(SynchoSectionTimeout, self).__init__(message) self.errors = errors class LockSynchronizedBlock(object): def __init__(self, lock=threading.Lock(), timeout=0): self.lock = lock self.timeout = timeout def __enter__(self, *args, **kwargs): if not self.lock.acquire(self.timeout): raise SynchoSectionTimeout('Timed out in synchronized, lock: %s.', str(self.lock)) def __exit__(self, *args, **kwargs): self.lock.release() class LockSynchronizedFunc(object): def __init__(self, func, lock=threading.Lock(), timeout=0): self.func = func self.blk = LockSynchronizedBlock(lock, timeout) def __call__(self, *args, **kwargs): with self.blk: return self.func(*args, **kwargs) @LockSynchronizedFunc def a_serealised_function_decorated(*args, **kwargs): print 'args:', args print 'kwargs:', kwargs def main(): # run as regular function a_serealised_function_decorated(1,3,4, a=1, b=2, c=3)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question