How to limit len(comm.kernel.iopub_thread._events) when the notebook is not connected
- Vorherrschende Sprache
- Python
- Sterne
- 734
- Forks
- 412
- Ø Merge
- 1 T. 5 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
In [`adaptive`](https://github.com/python-adaptive/adaptive) we have a widget that displays some information about the calculation that is run.
The widget is driven by a coroutine an updated every ~0.1 seconds.
Now when you run this on a Jupyterhub and you are not connected to the notebook for several hours, and then connect again, the widget has to go through all the updates even though they are not relevant. This can take minutes.
[We came up with the following solution](https://gitlab.kwant-project.org/qt/adaptive/merge_requests/108/diffs):
```python
# `status` is the widget
def should_update(status):
# Get the length of the write buffer size
buffer_size = len(status.comm.kernel.iopub_thread._events)
# Make sure to only keep all the messages when the notebook
# is viewed, this means 'buffer_size == 1'. However, when not
# viewing the notebook the buffer fills up. When this happens
# we decide to only add messages to it when a certain probability.
# i.e. we're offline for 12h, with an update_interval of 0.5s,
# and without the reduced probability, we have buffer_size=86400.
# With the correction this is np.log(86400) / np.log(1.1) = 119.2
return 1.1**buffer_size * random.random() < 1
async def update():
if should_update(status):
status.value = update_widget(runner)
else:
await asyncio.sleep(0.05)
```
This seems to work fine, however we are using a private API `comm.kernel.iopub_thread._events `. Is there a better way of doing this that doesn't depend on private attributes?
Beitragsleitfaden
Rechercherichtung
Start by tracing the status.comm.kernel.iopub_thread._events access and the should_update/update entry points described in the issue. Compare the available public kernel and comm interfaces, then determine whether a supported way to limit queued updates can be exposed; done means the widget can avoid replaying irrelevant offline updates without relying on the private _events attribute.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- jupyter-notebook, python
- Bereich
- api, backend
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100