Use a timeutils.StopWatch for cancel timing

Instead of using time.time and such directly the
oslo utils timeutils code provides a handy timer that
itself uses monotonic time (so it won't be affected
by time shifts) so prefer to use that for the timer like
code that exists in this new cancel code.

Change-Id: Ie41f8e90cf1cb3f62fd0def00b0bc7068acd2c40
This commit is contained in:
Joshua Harlow 2016-03-17 09:46:12 -07:00
parent 18b0f38945
commit 9604fc9c43
1 changed files with 5 additions and 5 deletions

View File

@ -11,16 +11,16 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging import logging
import threading import threading
import time
import eventlet import eventlet
from eventlet import greenpool from eventlet import greenpool
from oslo_service._i18n import _LE from oslo_service._i18n import _LE
from oslo_service import loopingcall from oslo_service import loopingcall
from oslo_utils import timeutils
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -178,10 +178,10 @@ class ThreadGroup(object):
if timeout is None: if timeout is None:
return return
wait_time = kwargs.get('wait_time', 1) wait_time = kwargs.get('wait_time', 1)
start = time.time() watch = timeutils.StopWatch(duration=timeout)
watch.start()
while self._any_threads_alive(): while self._any_threads_alive():
run_time = time.time() - start if not watch.expired():
if run_time < timeout:
eventlet.sleep(wait_time) eventlet.sleep(wait_time)
continue continue
LOG.debug("Cancel timeout reached, stopping threads.") LOG.debug("Cancel timeout reached, stopping threads.")