diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-06-10 15:17:40 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-06-16 15:10:33 +0000 |
commit | b93891dab13a9e910755139d304ac8e18d6889fb (patch) | |
tree | 52d42fbbe49cf7e4b54a1599efe563c7a7e4853d | |
parent | f1bd24fbe47cc0d8103ce5652b6952108de372f2 (diff) | |
download | taskflow-b93891dab13a9e910755139d304ac8e18d6889fb.tar.gz |
Use monotonic lib. to avoid finding monotonic time function
That library already does this same/equivalent search and
ensures that a monotonically increasing time is made available
so we can just avoid looking around for it ourselves.
Change-Id: I2b5c69430d75a095f6743c10e2d9281f7d9120e0
-rw-r--r-- | requirements.txt | 3 | ||||
-rw-r--r-- | taskflow/types/periodic.py | 11 | ||||
-rw-r--r-- | taskflow/utils/misc.py | 29 |
3 files changed, 7 insertions, 36 deletions
diff --git a/requirements.txt b/requirements.txt index e9d0550..d0e7c91 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,6 +28,9 @@ stevedore>=1.3.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.0.0,<3.0.0 diff --git a/taskflow/types/periodic.py b/taskflow/types/periodic.py index 7314988..b658d49 100644 --- a/taskflow/types/periodic.py +++ b/taskflow/types/periodic.py @@ -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: diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index 005f394..42e2de5 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -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. |