33import asyncio
44from contextlib import suppress
55import datetime
6+ import random
67import warnings
78
89
@@ -150,6 +151,24 @@ def cancel(_):
150151 return dm
151152
152153
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+
153172def live_info (runner , * , update_interval = 0.5 ):
154173 """Display live information about the runner.
155174
@@ -172,7 +191,12 @@ def live_info(runner, *, update_interval=0.5):
172191 async def update ():
173192 while not runner .task .done ():
174193 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+
176200 status .value = _info_html (runner )
177201 cancel .layout .display = 'none'
178202
0 commit comments