3
3
import asyncio
4
4
from contextlib import suppress
5
5
import datetime
6
+ import random
6
7
import warnings
7
8
8
9
@@ -150,6 +151,24 @@ def cancel(_):
150
151
return dm
151
152
152
153
154
+ def should_update (status ):
155
+ try :
156
+ # Get the length of the write buffer size
157
+ buffer_size = len (status .comm .kernel .iopub_thread ._events )
158
+
159
+ # Make sure to only keep all the messages when the notebook
160
+ # is viewed, this means 'buffer_size == 1'. However, when not
161
+ # viewing the notebook the buffer fills up. When this happens
162
+ # we decide to only add messages to it when a certain probability.
163
+ # i.e. we're offline for 12h, with an update_interval of 0.5s,
164
+ # and without the reduced probability, we have buffer_size=86400.
165
+ # With the correction this is np.log(86400) / np.log(1.1) = 119.2
166
+ return 1.1 ** buffer_size * random .random () < 1
167
+ except Exception :
168
+ # We catch any Exception because we are using a private API.
169
+ return True
170
+
171
+
153
172
def live_info (runner , * , update_interval = 0.5 ):
154
173
"""Display live information about the runner.
155
174
@@ -172,7 +191,12 @@ def live_info(runner, *, update_interval=0.5):
172
191
async def update ():
173
192
while not runner .task .done ():
174
193
await asyncio .sleep (update_interval )
175
- status .value = _info_html (runner )
194
+
195
+ if should_update (status ):
196
+ status .value = _info_html (runner )
197
+ else :
198
+ await asyncio .sleep (0.05 )
199
+
176
200
status .value = _info_html (runner )
177
201
cancel .layout .display = 'none'
178
202
0 commit comments