Avoid timer drift during long callbacks by greglucas · Pull Request #29023 · matplotlib/matplotlib · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/matplotlib/backend_bases.py
22 changes: 21 additions & 1 deletion lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os.path
import pathlib
import sys
import time
import tkinter as tk
import tkinter.filedialog
import tkinter.font
Expand Down Expand Up @@ -132,21 +133,40 @@ def __init__(self, parent, *args, **kwargs):
def _timer_start(self):
self._timer_stop()
self._timer = self.parent.after(self._interval, self._on_timer)
# Keep track of the firing time for repeating timers since
# we have to do this manually in Tk
self._timer_start_count = time.perf_counter_ns()

def _timer_stop(self):
if self._timer is not None:
self.parent.after_cancel(self._timer)
self._timer = None

def _on_timer(self):
# We want to measure the time spent in the callback, so we need to
# record the time before calling the base class method.
timer_fire_ms = (time.perf_counter_ns() - self._timer_start_count) // 1_000_000
super()._on_timer()
# Tk after() is only a single shot, so we need to add code here to
# reset the timer if we're not operating in single shot mode. However,
# if _timer is None, this means that _timer_stop has been called; so
# don't recreate the timer in that case.
if not self._single and self._timer:
if self._interval > 0:
self._timer = self.parent.after(self._interval, self._on_timer)
# We want to adjust our fire time independent of the time
# spent in the callback and not drift over time, so reference
# to the start count.
after_callback_ms = ((time.perf_counter_ns() - self._timer_start_count)
// 1_000_000)
if after_callback_ms - timer_fire_ms < self._interval:
next_interval = self._interval - after_callback_ms % self._interval
# minimum of 1ms
next_interval = max(1, next_interval)
else:
# Account for the callback being longer than the interval, where
# we really want to fire the next timer as soon as possible.
next_interval = 1
self._timer = self.parent.after(next_interval, self._on_timer)
else:
# Edge case: Tcl after 0 *prepends* events to the queue
# so a 0 interval does not allow any other events to run.
Expand Down
24 changes: 23 additions & 1 deletion lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import importlib
from unittest.mock import patch

from matplotlib import path, transforms
from matplotlib.backend_bases import (
FigureCanvasBase, KeyEvent, LocationEvent, MouseButton, MouseEvent,
NavigationToolbar2, RendererBase)
NavigationToolbar2, RendererBase, TimerBase)
from matplotlib.backend_tools import RubberbandBase
from matplotlib.figure import Figure
from matplotlib.testing._markers import needs_pgf_xelatex
Expand Down Expand Up @@ -581,3 +582,24 @@ def test_interactive_pan_zoom_events(tool, button, patch_vis, forward_nav, t_s):
# Check if twin-axes are properly triggered
assert ax_t.get_xlim() == pytest.approx(ax_t_twin.get_xlim(), abs=0.15)
assert ax_b.get_xlim() == pytest.approx(ax_b_twin.get_xlim(), abs=0.15)


def test_timer_properties():
# Setting a property to the same value should not trigger the
# private setter call again.
timer = TimerBase(100)
with patch.object(timer, '_timer_set_interval') as mock:
timer.interval = 200
mock.assert_called_once()
assert timer.interval == 200
timer.interval = 200
# Make sure it wasn't called again
mock.assert_called_once()

with patch.object(timer, '_timer_set_single_shot') as mock:
timer.single_shot = True
mock.assert_called_once()
assert timer._single
timer.single_shot = True
# Make sure it wasn't called again
mock.assert_called_once()
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_backends_interactive.py