Issue #7316: the acquire() method of lock objects in the :mod:threading

module now takes an optional timeout argument in seconds.  Timeout support
relies on the system threading library, so as to avoid a semi-busy wait
loop.
This commit is contained in:
Antoine Pitrou 2010-04-14 15:44:10 +00:00
parent e53de3dc4a
commit 7c3e577395
11 changed files with 326 additions and 77 deletions

View file

@ -17,6 +17,10 @@ __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
'interrupt_main', 'LockType']
import traceback as _traceback
import time
# A dummy value
TIMEOUT_MAX = 2**31
class error(Exception):
"""Dummy implementation of _thread.error."""
@ -92,7 +96,7 @@ class LockType(object):
def __init__(self):
self.locked_status = False
def acquire(self, waitflag=None):
def acquire(self, waitflag=None, timeout=-1):
"""Dummy implementation of acquire().
For blocking calls, self.locked_status is automatically set to
@ -111,6 +115,8 @@ class LockType(object):
self.locked_status = True
return True
else:
if timeout > 0:
time.sleep(timeout)
return False
__enter__ = acquire