Merge "Use monotonic lib. to avoid finding monotonic time function"

This commit is contained in:
Jenkins
2015-06-24 15:23:23 +00:00
committed by Gerrit Code Review
3 changed files with 7 additions and 36 deletions

View File

@@ -31,6 +31,9 @@ stevedore>=1.5.0 # Apache-2.0
# Backport for concurrent.futures which exists in 3.2+
futures>=3.0
# Backport for time.monotonic which is in 3.3+
monotonic>=0.1
# Used for structured input validation
jsonschema!=2.5.0,<3.0.0,>=2.0.0

View File

@@ -18,6 +18,7 @@ import heapq
import inspect
from debtcollector import removals
import monotonic
from oslo_utils import reflection
import six
@@ -27,10 +28,6 @@ from taskflow.utils import threading_utils as tu
LOG = logging.getLogger(__name__)
# Find a monotonic providing time (or fallback to using time.time()
# which isn't *always* accurate but will suffice).
_now = misc.find_monotonic(allow_time_time=True)
def _check_attrs(obj):
"""Checks that a periodic function/method has all the expected attributes.
@@ -81,7 +78,7 @@ class _Schedule(object):
def push_next(self, cb, index, now=None):
if now is None:
now = _now()
now = monotonic.monotonic()
self.push(now + cb._periodic_spacing, index)
def __len__(self):
@@ -103,7 +100,7 @@ def _build(callables):
immediates.append(i)
else:
if now is None:
now = _now()
now = monotonic.monotonic()
schedule.push_next(cb, i, now=now)
return immediates, schedule
@@ -187,7 +184,7 @@ class PeriodicWorker(object):
# minimum item from the heap, where the minimum should be
# the callable that needs to run next and has the lowest
# next desired run time).
now = _now()
now = monotonic.monotonic()
next_run, index = self._schedule.pop()
when_next = next_run - now
if when_next <= 0:

View File

@@ -24,7 +24,6 @@ import os
import re
import sys
import threading
import time
import types
import enum
@@ -48,17 +47,6 @@ NUMERIC_TYPES = six.integer_types + (float,)
# see RFC 3986 section 3.1
_SCHEME_REGEX = re.compile(r"^([A-Za-z][A-Za-z0-9+.-]*):")
_MONOTONIC_LOCATIONS = tuple([
# The built-in/expected location in python3.3+
'time.monotonic',
# NOTE(harlowja): Try to use the pypi module that provides this
# functionality for older versions of python less than 3.3 so that
# they to can benefit from better timing...
#
# See: http://pypi.python.org/pypi/monotonic
'monotonic.monotonic',
])
class StrEnum(str, enum.Enum):
"""An enumeration that is also a string and can be compared to strings."""
@@ -71,23 +59,6 @@ class StrEnum(str, enum.Enum):
return super(StrEnum, cls).__new__(cls, *args, **kwargs)
def find_monotonic(allow_time_time=False):
"""Tries to find a monotonic time providing function (and returns it)."""
for import_str in _MONOTONIC_LOCATIONS:
mod_str, _sep, attr_str = import_str.rpartition('.')
mod = importutils.try_import(mod_str)
if mod is None:
continue
func = getattr(mod, attr_str, None)
if func is not None:
return func
# Finally give up and use time.time (which isn't monotonic)...
if allow_time_time:
return time.time
else:
return None
def match_type(obj, matchers):
"""Matches a given object using the given matchers list/iterable.