|
10 | 10 | # - `backend_webagg.py` contains a concrete implementation of a basic |
11 | 11 | # application, implemented with asyncio. |
12 | 12 |
|
| 13 | +import asyncio |
| 14 | +import datetime |
13 | 15 | from io import BytesIO, StringIO |
14 | 16 | import json |
15 | 17 | import logging |
|
18 | 20 |
|
19 | 21 | import numpy as np |
20 | 22 | from PIL import Image |
21 | | -import asyncio |
22 | 23 |
|
23 | 24 | from matplotlib import _api, backend_bases, backend_tools |
24 | 25 | from matplotlib.backends import backend_agg |
@@ -78,6 +79,45 @@ def _handle_key(key): |
78 | 79 | return key |
79 | 80 |
|
80 | 81 |
|
| 82 | +class TimerTornado(backend_bases.TimerBase): |
| 83 | + def __init__(self, *args, **kwargs): |
| 84 | + self._timer = None |
| 85 | + super().__init__(*args, **kwargs) |
| 86 | + |
| 87 | + def _timer_start(self): |
| 88 | + import tornado |
| 89 | + |
| 90 | + self._timer_stop() |
| 91 | + if self._single: |
| 92 | + ioloop = tornado.ioloop.IOLoop.instance() |
| 93 | + self._timer = ioloop.add_timeout( |
| 94 | + datetime.timedelta(milliseconds=self.interval), |
| 95 | + self._on_timer) |
| 96 | + else: |
| 97 | + self._timer = tornado.ioloop.PeriodicCallback( |
| 98 | + self._on_timer, |
| 99 | + max(self.interval, 1e-6)) |
| 100 | + self._timer.start() |
| 101 | + |
| 102 | + def _timer_stop(self): |
| 103 | + import tornado |
| 104 | + |
| 105 | + if self._timer is None: |
| 106 | + return |
| 107 | + elif self._single: |
| 108 | + ioloop = tornado.ioloop.IOLoop.instance() |
| 109 | + ioloop.remove_timeout(self._timer) |
| 110 | + else: |
| 111 | + self._timer.stop() |
| 112 | + self._timer = None |
| 113 | + |
| 114 | + def _timer_set_interval(self): |
| 115 | + # Only stop and restart it if the timer has already been started |
| 116 | + if self._timer is not None: |
| 117 | + self._timer_stop() |
| 118 | + self._timer_start() |
| 119 | + |
| 120 | + |
81 | 121 | class TimerAsyncio(backend_bases.TimerBase): |
82 | 122 | def __init__(self, *args, **kwargs): |
83 | 123 | self._task = None |
|
0 commit comments