#NEWLINE# def acquire_semaphore( conn , semname , limit , timeout = 10 ) : #NEWLINE# #INDENT# identifier = str ( uuid . uuid4 ( ) ) #NEWLINE# now = time . time ( ) #NEWLINE# pipeline = conn . pipeline ( True ) #NEWLINE# pipeline . zremrangebyscore ( semname , '-inf' , ( now - timeout ) ) #NEWLINE# pipeline . zadd ( semname , identifier , now ) #NEWLINE# pipeline . zrank ( semname , identifier ) #NEWLINE# if ( pipeline . execute ( ) [ ( - 1 ) ] < limit ) : #NEWLINE# #INDENT# return identifier #NEWLINE# #UNINDENT# conn . zrem ( semname , identifier ) #NEWLINE# return None