<s> import threading <EOL> import traceback <EOL> class _ThreadedFunc ( threading . Thread ) : <EOL> def __init__ ( self , func , otherself , args , kargs , resource_manager , logging ) : <EOL> super ( _ThreadedFunc , self ) . __init__ ( ) <EOL> self . _self = otherself <EOL> self . _args = args <EOL> self . _kargs = kargs <EOL> self . _func = func <EOL> self . _resource_manager = resource_manager <EOL> self . result = None <EOL> self . raised_exc = None <EOL> self . finished_ok = False <EOL> self . logging = logging <EOL> if self . _resource_manager != None : <EOL> self . _resource_manager . add_resource ( self ) <EOL> def run ( self ) : <EOL> try : <EOL> try : <EOL> self . result = self . _func ( <EOL> self . _self , <EOL> * self . _args , <EOL> ** self . _kargs <EOL> ) <EOL> self . finished_ok = True <EOL> finally : <EOL> if self . _resource_manager != None : <EOL> self . _resource_manager . remove_resource ( self ) <EOL> except Exception , e : <EOL> self . raised_exc = e <EOL> print e <EOL> traceback . print_exc ( ) <EOL> _global_threaded_thread = threading . Lock ( ) <EOL> def threaded ( resource_manager = None , logging = True ) : <EOL> """<STR_LIT>""" <EOL> def wrapped_threaded ( func ) : <EOL> def auxiliar_func ( self , threaded_func ) : <EOL> self . _threaded_threads_lock . acquire ( ) <EOL> try : <EOL> self . _threaded_threads = [ i for i in self . _threaded_threads if i . isAlive ( ) ] <EOL> self . _threaded_threads . append ( threaded_func ) <EOL> finally : <EOL> self . _threaded_threads_lock . release ( ) <EOL> def wrapped_func ( self , * args , ** kargs ) : <EOL> threaded_func = _ThreadedFunc ( func , self , args , kargs , resource_manager , logging ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> auxiliar_func ( self , threaded_func ) <EOL> else : <EOL> _global_threaded_thread . acquire ( ) <EOL> try : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> auxiliar_func ( self , threaded_func ) <EOL> else : <EOL> self . _threaded_threads_lock = threading . Lock ( ) <EOL> self . _threaded_threads_lock . acquire ( ) <EOL> try : <EOL> self . _threaded_threads = [ threaded_func ] <EOL> finally : <EOL> self . _threaded_threads_lock . release ( ) <EOL> finally : <EOL> _global_threaded_thread . release ( ) <EOL> threaded_func . start ( ) <EOL> return threaded_func <EOL> wrapped_func . __doc__ = func . __doc__ <EOL> wrapped_func . __name__ = func . __name__ <EOL> return wrapped_func <EOL> return wrapped_threaded <EOL> def synchronized ( obj = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) . __name__ not in ( threading . Lock . __name__ , threading . RLock . __name__ ) : <EOL> lk = threading . Lock ( ) <EOL> obj . __dict__ [ "<STR_LIT>" ] = lk <EOL> obj = lk <EOL> def wrap_lock ( f ) : <EOL> def newFunction ( * args , ** kw ) : <EOL> obj . acquire ( ) <EOL> try : <EOL> return f ( * args , ** kw ) <EOL> finally : <EOL> obj . release ( ) <EOL> return newFunction <EOL> return wrap_lock </s>
