Skip to content

Commit

Permalink
Use time.monotonic() for certain timing operations.
Browse files Browse the repository at this point in the history
Refs #521
  • Loading branch information
coleifer committed May 18, 2020
1 parent 4ccc4d5 commit 76bb933
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 5 additions & 4 deletions huey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from huey.utils import normalize_time
from huey.utils import reraise_as
from huey.utils import string_type
from huey.utils import time_clock
from huey.utils import to_timestamp


Expand Down Expand Up @@ -350,15 +351,15 @@ def _execute(self, task, timestamp):
self._emit(S.SIGNAL_CANCELED, task)
return

start = time.time()
start = time_clock()
exception = None
task_value = None

try:
try:
task_value = task.execute()
finally:
duration = time.time() - start
duration = time_clock() - start
except TaskLockedException as exc:
logger.warning('Task %s not run, unable to acquire lock.', task.id)
exception = exc
Expand Down Expand Up @@ -872,10 +873,10 @@ def get_raw_result(self, blocking=False, timeout=None, backoff=1.15,
if res is not EmptyData:
return res
else:
start = time.time()
start = time_clock()
delay = .1
while self._result is EmptyData:
if timeout and time.time() - start >= timeout:
if timeout and time_clock() - start >= timeout:
if revoke_on_timeout:
self.revoke()
raise HueyException('timed out waiting for result')
Expand Down
9 changes: 5 additions & 4 deletions huey/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from huey.constants import WORKER_THREAD
from huey.constants import WORKER_TYPES
from huey.exceptions import ConfigurationError
from huey.utils import time_clock


class BaseProcess(object):
Expand Down Expand Up @@ -167,11 +168,11 @@ def loop(self, now=None):
current_p = self._next_periodic
if current_p <= time.time():
self._next_periodic += 60
self.enqueue_periodic_tasks(now, current)
self.enqueue_periodic_tasks(now)

self.sleep_for_interval(current, self.interval)

def enqueue_periodic_tasks(self, now, start):
def enqueue_periodic_tasks(self, now):
self._logger.debug('Checking periodic tasks')
for task in self.huey.read_periodic(now):
self._logger.info('Enqueueing periodic task %s.', task)
Expand Down Expand Up @@ -419,7 +420,7 @@ def run(self):
"""
self.start()
timeout = self._stop_flag_timeout
health_check_ts = time.time()
health_check_ts = time_clock()

while True:
try:
Expand All @@ -438,7 +439,7 @@ def run(self):
break

if self._health_check:
now = time.time()
now = time_clock()
if now >= health_check_ts + self._health_check_interval:
health_check_ts = now
self.check_worker_health()
Expand Down
6 changes: 6 additions & 0 deletions huey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,9 @@ def __enter__(self):

def __exit__(self, exc_type, exc_val, exc_tb):
self.release()


if sys.version_info[0] < 3:
time_clock = time.time
else:
time_clock = time.monotonic

0 comments on commit 76bb933

Please sign in to comment.