Skip to content

Commit a6dae43

Browse files
committed
Merge branch 'limit_message_queue' into 'master'
exponentially decay message frequency in live_info See merge request qt/adaptive!108
2 parents 6cdd24e + 7f936b7 commit a6dae43

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

adaptive/notebook_integration.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
from contextlib import suppress
55
import datetime
6+
import random
67
import 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+
153172
def 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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
import ipykernel.iostream
3+
import zmq
4+
5+
6+
def test_private_api_used_in_live_info():
7+
"""We are catching all errors in
8+
adaptive.notebook_integration.should_update
9+
so if ipykernel changed its API it would happen unnoticed."""
10+
# XXX: find a potential better solution in
11+
# https://github.com/ipython/ipykernel/issues/365
12+
ctx = zmq.Context()
13+
iopub_socket = ctx.socket(zmq.PUB)
14+
iopub_thread = ipykernel.iostream.IOPubThread(iopub_socket)
15+
assert hasattr(iopub_thread, '_events')

0 commit comments

Comments
 (0)