Skip to content

AsyncRunner fails on Python 3.14 due to asyncio.get_event_loop() removal #489

@basnijholt

Description

@basnijholt

Description

AsyncRunner fails to instantiate on Python 3.14 with a RuntimeError because asyncio.get_event_loop() now raises an error when no event loop is running.

Reproduction

from adaptive import Learner1D
from adaptive.runner import AsyncRunner

def f(x):
    return x

learner = Learner1D(f, (-1, 1))
runner = AsyncRunner(learner, npoints_goal=10)

Error on Python 3.14:

RuntimeError: There is no current event loop in thread 'MainThread'.

Cause

In adaptive/runner.py line 629:

self.ioloop = ioloop or asyncio.get_event_loop()

The behavior of asyncio.get_event_loop() changed across Python versions:

  • Python 3.10: Deprecated when no running event loop exists
  • Python 3.12: Emits DeprecationWarning
  • Python 3.14: Raises RuntimeError

Proposed Fix

Replace with a version-agnostic approach:

if ioloop is not None:
    self.ioloop = ioloop
else:
    try:
        self.ioloop = asyncio.get_running_loop()
    except RuntimeError:
        self.ioloop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.ioloop)

This works on Python 3.7+ and handles all cases correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions