Skip to content

Commit 156778d

Browse files
authored
Fix AsyncRunner failure on Python 3.14 due to asyncio.get_event_loop() removal (#490)
1 parent be769d8 commit 156778d

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

.github/workflows/nox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
fail-fast: false
1313
matrix:
1414
platform: [ubuntu-latest, macos-latest, windows-latest]
15-
python-version: ["3.11", "3.12", "3.13"]
15+
python-version: ["3.11", "3.12", "3.13", "3.14"]
1616

1717
steps:
1818
- uses: actions/checkout@v4

adaptive/runner.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def __init__(
626626
raise_if_retries_exceeded=raise_if_retries_exceeded,
627627
allow_running_forever=True,
628628
)
629-
self.ioloop = ioloop or asyncio.get_event_loop()
629+
self.ioloop = ioloop if ioloop is not None else _get_or_create_event_loop()
630630

631631
# When the learned function is 'async def', we run it
632632
# directly on the event loop, and not in the executor.
@@ -987,7 +987,22 @@ def replay_log(
987987
getattr(learner, method)(*args)
988988

989989

990-
# -- Internal executor-related, things
990+
# -- Internal executor-related things
991+
992+
993+
def _get_or_create_event_loop() -> asyncio.AbstractEventLoop:
994+
"""Get the running event loop or create a new one.
995+
996+
In Python 3.10+, asyncio.get_event_loop() is deprecated when no loop is running.
997+
In Python 3.14+, it raises RuntimeError instead of creating a new loop.
998+
This function provides a compatible way to get or create an event loop.
999+
"""
1000+
try:
1001+
return asyncio.get_running_loop()
1002+
except RuntimeError:
1003+
loop = asyncio.new_event_loop()
1004+
asyncio.set_event_loop(loop)
1005+
return loop
9911006

9921007

9931008
def _ensure_executor(executor: ExecutorTypes | None) -> concurrent.Executor:

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
nox.options.default_venv_backend = "uv"
88

9-
python = ["3.11", "3.12", "3.13"]
9+
python = ["3.11", "3.12", "3.13", "3.14"]
1010
num_cpus = os.cpu_count() or 1
1111
xdist = ("-n", "auto") if num_cpus > 2 else ()
1212

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ classifiers = [
1616
"Programming Language :: Python :: 3.11",
1717
"Programming Language :: Python :: 3.12",
1818
"Programming Language :: Python :: 3.13",
19+
"Programming Language :: Python :: 3.14",
1920
]
2021
dependencies = [
2122
"scipy",

0 commit comments

Comments
 (0)