Skip to content

Commit

Permalink
Use monotonic clock for timing scheduler loop interval.
Browse files Browse the repository at this point in the history
Refs and resolves #521
  • Loading branch information
coleifer committed May 18, 2020
1 parent 76bb933 commit 1f14785
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 6 additions & 6 deletions huey/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def sleep_for_interval(self, start_ts, nseconds):
if the current timestamp is 1340, we'll only sleep for 7 seconds (the
goal being to sleep until 1347, or 1337 + 10).
"""
sleep_time = nseconds - (time.time() - start_ts)
sleep_time = nseconds - (time_clock() - start_ts)
if sleep_time <= 0:
return
self._logger.debug('Sleeping for %s', sleep_time)
# Recompute time to sleep to improve accuracy in case the process was
# pre-empted by the kernel while logging.
sleep_time = nseconds - (time.time() - start_ts)
sleep_time = nseconds - (time_clock() - start_ts)
if sleep_time > 0:
time.sleep(sleep_time)

Expand Down Expand Up @@ -145,13 +145,13 @@ def __init__(self, huey, interval, periodic):
self.interval = min(interval, 60)

self.periodic = periodic
self._next_loop = time.time()
self._next_periodic = time.time()
self._next_loop = time_clock()
self._next_periodic = time_clock()

def loop(self, now=None):
current = self._next_loop
self._next_loop += self.interval
if self._next_loop < time.time():
if self._next_loop < time_clock():
self._logger.debug('scheduler skipping iteration to avoid race.')
return

Expand All @@ -166,7 +166,7 @@ def loop(self, now=None):

if self.periodic:
current_p = self._next_periodic
if current_p <= time.time():
if current_p <= time_clock():
self._next_periodic += 60
self.enqueue_periodic_tasks(now)

Expand Down
5 changes: 3 additions & 2 deletions huey/tests/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from huey.consumer import Scheduler
from huey.consumer_options import ConsumerConfig
from huey.tests.base import BaseTestCase
from huey.utils import time_clock


class TestConsumer(Consumer):
Expand Down Expand Up @@ -35,8 +36,8 @@ def work_on_tasks(self, consumer, n=1, now=None):

def schedule_tasks(self, consumer, now=None):
scheduler = consumer._create_scheduler()
scheduler._next_loop = time.time() + 60
scheduler._next_periodic = time.time() - 60
scheduler._next_loop = time_clock() + 60
scheduler._next_periodic = time_clock() - 60
scheduler.loop(now)

def test_consumer_schedule_task(self):
Expand Down

0 comments on commit 1f14785

Please sign in to comment.