♻ Use monotonic_time for relative times
Timeouts, durations and such shouldn't depend on the non-monotonic system clock but instead use a monotonic time source.
This commit is contained in:
1 parent
93ab7bb089
commit
76ad332287
12 files changed
+68
-70
No files matched your search
@@ -18,7 +18,7 @@ from serial import SerialTimeoutException
|
||||
|
||||
from octoprint.settings import settings
|
||||
from octoprint.plugin import plugin_manager
|
||||
from octoprint.util import RepeatedTimer
|
||||
from octoprint.util import RepeatedTimer, monotonic_time
|
||||
|
||||
class VirtualPrinter(object):
|
||||
command_regex = re.compile("^([GMTF])(\d+)")
|
||||
@@ -84,7 +84,7 @@ class VirtualPrinter(object):
|
||||
self.targetTemp = [0.0] * self.temperatureCount
|
||||
self.bedTemp = self._ambient_temperature
|
||||
self.bedTargetTemp = 0.0
|
||||
self.lastTempAt = time.time()
|
||||
self.lastTempAt = monotonic_time()
|
||||
|
||||
self._relative = True
|
||||
self._lastX = 0.0
|
||||
@@ -264,7 +264,7 @@ class VirtualPrinter(object):
|
||||
pass
|
||||
|
||||
def _processIncoming(self):
|
||||
next_wait_timeout = time.time() + self._waitInterval
|
||||
next_wait_timeout = monotonic_time() + self._waitInterval
|
||||
buf = ""
|
||||
while self.incoming is not None and not self._killed:
|
||||
self._simulateTemps()
|
||||
@@ -277,9 +277,9 @@ class VirtualPrinter(object):
|
||||
data = self.incoming.get(timeout=0.01)
|
||||
self.incoming.task_done()
|
||||
except queue.Empty:
|
||||
if self._sendWait and time.time() > next_wait_timeout:
|
||||
if self._sendWait and monotonic_time() > next_wait_timeout:
|
||||
self._send("wait")
|
||||
next_wait_timeout = time.time() + self._waitInterval
|
||||
next_wait_timeout = monotonic_time() + self._waitInterval
|
||||
continue
|
||||
|
||||
buf += data
|
||||
@@ -289,7 +289,7 @@ class VirtualPrinter(object):
|
||||
else:
|
||||
continue
|
||||
|
||||
next_wait_timeout = time.time() + self._waitInterval
|
||||
next_wait_timeout = monotonic_time() + self._waitInterval
|
||||
|
||||
if data is None:
|
||||
continue
|
||||
@@ -640,8 +640,8 @@ class VirtualPrinter(object):
|
||||
_timeout = float(matchS.group(1))
|
||||
|
||||
if self._sendBusy and self._busyInterval > 0:
|
||||
until = time.time() + _timeout
|
||||
while time.time() < until:
|
||||
until = monotonic_time() + _timeout
|
||||
while monotonic_time() < until:
|
||||
time.sleep(self._busyInterval)
|
||||
self._send("busy:processing")
|
||||
else:
|
||||
@@ -653,8 +653,8 @@ class VirtualPrinter(object):
|
||||
timeout = 60
|
||||
|
||||
if self._sendBusy and self._busyInterval > 0:
|
||||
until = time.time() + timeout
|
||||
while time.time() < until:
|
||||
until = monotonic_time() + timeout
|
||||
while monotonic_time() < until:
|
||||
time.sleep(self._busyInterval)
|
||||
self._send("busy:processing")
|
||||
else:
|
||||
@@ -1199,7 +1199,7 @@ class VirtualPrinter(object):
|
||||
def _waitForHeatup(self, heater, only_wait_if_higher):
|
||||
delta = 1
|
||||
delay = 1
|
||||
last_busy = time.time()
|
||||
last_busy = monotonic_time()
|
||||
|
||||
self._heatingUp = True
|
||||
try:
|
||||
@@ -1216,9 +1216,9 @@ class VirtualPrinter(object):
|
||||
while not self._killed and self._heatingUp and test():
|
||||
self._simulateTemps(delta=delta)
|
||||
self._output(output())
|
||||
if self._sendBusy and time.time() - last_busy >= self._busyInterval:
|
||||
if self._sendBusy and monotonic_time() - last_busy >= self._busyInterval:
|
||||
self._output("echo:busy: processing")
|
||||
last_busy = time.time()
|
||||
last_busy = monotonic_time()
|
||||
time.sleep(delay)
|
||||
except AttributeError:
|
||||
if self.outgoing is not None:
|
||||
@@ -1234,8 +1234,8 @@ class VirtualPrinter(object):
|
||||
os.remove(f)
|
||||
|
||||
def _simulateTemps(self, delta=0.5):
|
||||
timeDiff = self.lastTempAt - time.time()
|
||||
self.lastTempAt = time.time()
|
||||
timeDiff = self.lastTempAt - monotonic_time()
|
||||
self.lastTempAt = monotonic_time()
|
||||
|
||||
def simulate(actual, target, ambient):
|
||||
if target > 0 and abs(actual - target) > delta:
|
||||
@@ -1409,9 +1409,9 @@ class CharCountingQueue(queue.Queue):
|
||||
elif timeout < 0:
|
||||
raise ValueError("'timeout' must be a positive number")
|
||||
else:
|
||||
endtime = time.time() + timeout
|
||||
endtime = monotonic_time() + timeout
|
||||
while not self._will_it_fit(item):
|
||||
remaining = endtime - time.time()
|
||||
remaining = endtime - monotonic_time()
|
||||
if remaining <= 0.0:
|
||||
raise queue.Full
|
||||
self.not_full.wait(remaining)
|
||||
|
||||
Reference in new issue
Block a user