Skip to content

fix(client): make sync OEClient usable in Jupyter notebooks#32

Merged
nc9 merged 1 commit into
mainfrom
fix/notebook-sync
May 25, 2026
Merged

fix(client): make sync OEClient usable in Jupyter notebooks#32
nc9 merged 1 commit into
mainfrom
fix/notebook-sync

Conversation

@nc9

@nc9 nc9 commented May 22, 2026

Copy link
Copy Markdown
Member

Closes #16

Problem

OEClient (sync) calls asyncio.run() internally. asyncio.run() raises RuntimeError: asyncio.run() cannot be called from a running event loop when a loop is already running — which is always the case in a Jupyter/IPython notebook. So the synchronous client, the one notebook users reach for first, fails there.

Fix

A _run_sync() helper:

  • no loop running -> asyncio.run() as before
  • loop already running (notebook) -> run the coroutine on its own loop in a worker thread

All OEClient methods and close() go through it. No API change, no new dependency (concurrent.futures is stdlib).

Verified

  • tests/test_run_sync.py covers both paths (with and without a running loop).
  • Manually: OEClient().get_facilities(...) called from inside asyncio.run(...) now succeeds (previously RuntimeError).
  • Full suite: 22 passed.

OEClient called asyncio.run() directly, which raises RuntimeError when a
loop is already running — so the sync client failed in Jupyter/IPython
notebooks. Route coroutines through _run_sync(), which falls back to a
worker thread when a loop is already active.

Fixes #16
@nc9 nc9 merged commit 18bae56 into main May 25, 2026
5 checks passed
@nc9 nc9 deleted the fix/notebook-sync branch May 25, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Package does not work in Jupyter/iPython Notebooks

1 participant