@@ -116,6 +116,7 @@ def acquire(
116
116
poll_interval : float = 0.05 ,
117
117
* ,
118
118
poll_intervall : float | None = None ,
119
+ blocking : bool = True ,
119
120
) -> AcquireReturnProxy :
120
121
"""
121
122
Try to acquire the file lock.
@@ -124,6 +125,8 @@ def acquire(
124
125
if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
125
126
:param poll_interval: interval of trying to acquire the lock file
126
127
: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.
127
130
:raises Timeout: if fails to acquire lock within the timeout period
128
131
:return: a context object that will unlock the file when the context is exited
129
132
@@ -172,6 +175,9 @@ def acquire(
172
175
if self .is_locked :
173
176
_LOGGER .debug ("Lock %s acquired on %s" , lock_id , lock_filename )
174
177
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 )
175
181
elif 0 <= timeout < time .monotonic () - start_time :
176
182
_LOGGER .debug ("Timeout on acquiring lock %s on %s" , lock_id , lock_filename )
177
183
raise Timeout (self ._lock_file )
0 commit comments