@@ -116,6 +116,7 @@ def acquire(
116116        poll_interval : float  =  0.05 ,
117117        * ,
118118        poll_intervall : float  |  None  =  None ,
119+         blocking : bool  =  True ,
119120    ) ->  AcquireReturnProxy :
120121        """ 
121122        Try to acquire the file lock. 
@@ -124,6 +125,8 @@ def acquire(
124125         if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired 
125126        :param poll_interval: interval of trying to acquire the lock file 
126127        :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead 
128+         :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the 
129+          first attempt. Otherwise this method will block until the timeout expires or the lock is acquired. 
127130        :raises Timeout: if fails to acquire lock within the timeout period 
128131        :return: a context object that will unlock the file when the context is exited 
129132
@@ -172,6 +175,9 @@ def acquire(
172175                if  self .is_locked :
173176                    _LOGGER .debug ("Lock %s acquired on %s" , lock_id , lock_filename )
174177                    break 
178+                 elif  blocking  is  False :
179+                     _LOGGER .debug ("Failed to immediately acquire lock %s on %s" , lock_id , lock_filename )
180+                     raise  Timeout (self ._lock_file )
175181                elif  0  <=  timeout  <  time .monotonic () -  start_time :
176182                    _LOGGER .debug ("Timeout on acquiring lock %s on %s" , lock_id , lock_filename )
177183                    raise  Timeout (self ._lock_file )
0 commit comments