#NEWLINE# def acquire_lock_with_timeout( conn , lockname , acquire_timeout = 10 , lock_timeout = 10 ) : #NEWLINE# #INDENT# identifier = str ( uuid . uuid4 ( ) ) #NEWLINE# lockname = ( 'lock:' + lockname ) #NEWLINE# lock_timeout = int ( math . ceil ( lock_timeout ) ) #NEWLINE# end = ( time . time ( ) + acquire_timeout ) #NEWLINE# while ( time . time ( ) < end ) : #NEWLINE# #INDENT# if conn . setnx ( lockname , identifier ) : #NEWLINE# #INDENT# conn . expire ( lockname , lock_timeout ) #NEWLINE# return identifier #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not conn . ttl ( lockname ) ) : #NEWLINE# #INDENT# conn . expire ( lockname , lock_timeout ) #NEWLINE# #UNINDENT# #UNINDENT# time . sleep ( 0.001 ) #NEWLINE# #UNINDENT# return False