From 7d61b6fbd4a17be60afe5fb0209d1f400b596705 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 9 Apr 2018 23:34:17 +0000 Subject: [PATCH] Add acuire_with_timeout(). --- tests/helpers/threading.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/helpers/threading.py b/tests/helpers/threading.py index 6f649a51..155ed334 100644 --- a/tests/helpers/threading.py +++ b/tests/helpers/threading.py @@ -1,9 +1,25 @@ from __future__ import absolute_import +import sys import threading +import time import warnings +if sys.version_info < (3,): + def acquire_with_timeout(lock, timeout): + segments = int(timeout * 10) + 1 + for _ in range(segments): + if lock.acquire(False): + return True + time.sleep(0.1) + else: + return False +else: + def acquire_with_timeout(lock, timeout): + return lock.acquire(timeout=timeout) + + def get_locked_and_waiter(timeout=1.0): _timeout = timeout lock = threading.Lock() @@ -12,7 +28,7 @@ def get_locked_and_waiter(timeout=1.0): def wait(timeout=_timeout, reason=None): if timeout is None: timeout = _timeout - if lock.acquire(timeout=timeout): + if acquire_with_timeout(lock, timeout): lock.release() else: msg = 'timed out waiting'