#NEWLINE# def synchronized( name , lock_file_prefix = None , external = False , lock_path = None ) : #NEWLINE# #INDENT# "Synchronization decorator.\n\n    Decorating a method like so::\n\n        @synchronized('mylock')\n        def foo(self, *args):\n           ...\n\n    ensures that only one thread will execute the foo method at a time.\n\n    Different methods can share the same lock::\n\n        @synchronized('mylock')\n        def foo(self, *args):\n           ...\n\n        @synchronized('mylock')\n        def bar(self, *args):\n           ...\n\n    This way only one of either foo or bar can be executing at a time.\n    " #NEWLINE# def wrap( f ) : #NEWLINE# #INDENT# @ functools . wraps ( f ) #NEWLINE# def inner( * args , **kwargs ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# with lock ( name , lock_file_prefix , external , lock_path ) : #NEWLINE# #INDENT# LOG . debug ( _ ( 'Got semaphore / lock "%(function)s"' ) , { #NEWLINE# #INDENT# 'function' : f . __name__ , #NEWLINE# #UNINDENT# } ) #NEWLINE# return f ( * args , ** kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# LOG . debug ( _ ( 'Semaphore / lock released "%(function)s"' ) , { #NEWLINE# #INDENT# 'function' : f . __name__ , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# return inner #NEWLINE# #UNINDENT# return wrap