8
8
# way over a web socket.
9
9
#
10
10
# - `backend_webagg.py` contains a concrete implementation of a basic
11
- # application, implemented with tornado .
11
+ # application, implemented with asyncio .
12
12
13
+ import asyncio
13
14
import datetime
14
15
from io import BytesIO , StringIO
15
16
import json
19
20
20
21
import numpy as np
21
22
from PIL import Image
22
- import tornado
23
23
24
24
from matplotlib import _api , backend_bases , backend_tools
25
25
from matplotlib .backends import backend_agg
@@ -85,6 +85,8 @@ def __init__(self, *args, **kwargs):
85
85
super ().__init__ (* args , ** kwargs )
86
86
87
87
def _timer_start (self ):
88
+ import tornado
89
+
88
90
self ._timer_stop ()
89
91
if self ._single :
90
92
ioloop = tornado .ioloop .IOLoop .instance ()
@@ -98,6 +100,8 @@ def _timer_start(self):
98
100
self ._timer .start ()
99
101
100
102
def _timer_stop (self ):
103
+ import tornado
104
+
101
105
if self ._timer is None :
102
106
return
103
107
elif self ._single :
@@ -114,8 +118,43 @@ def _timer_set_interval(self):
114
118
self ._timer_start ()
115
119
116
120
121
+ class TimerAsyncio (backend_bases .TimerBase ):
122
+ def __init__ (self , * args , ** kwargs ):
123
+ self ._task = None
124
+ super ().__init__ (* args , ** kwargs )
125
+
126
+ async def _timer_task (self , interval ):
127
+ while True :
128
+ try :
129
+ await asyncio .sleep (interval )
130
+ self ._on_timer ()
131
+
132
+ if self ._single :
133
+ break
134
+ except asyncio .CancelledError :
135
+ break
136
+
137
+ def _timer_start (self ):
138
+ self ._timer_stop ()
139
+
140
+ self ._task = asyncio .ensure_future (
141
+ self ._timer_task (max (self .interval / 1_000. , 1e-6 ))
142
+ )
143
+
144
+ def _timer_stop (self ):
145
+ if self ._task is not None :
146
+ self ._task .cancel ()
147
+ self ._task = None
148
+
149
+ def _timer_set_interval (self ):
150
+ # Only stop and restart it if the timer has already been started
151
+ if self ._task is not None :
152
+ self ._timer_stop ()
153
+ self ._timer_start ()
154
+
155
+
117
156
class FigureCanvasWebAggCore (backend_agg .FigureCanvasAgg ):
118
- _timer_cls = TimerTornado
157
+ _timer_cls = TimerAsyncio
119
158
# Webagg and friends having the right methods, but still
120
159
# having bugs in practice. Do not advertise that it works until
121
160
# we can debug this.
0 commit comments