Backported Python 3.14 asyncio fix from dee0b942

Python 3.14 deprecated asyncio.get_event_loop() when there is no running
event loop. This backports the run_sync() helper function and its usage
from main branch to fix compatibility.

Refs #2506
This commit is contained in:
Simon Willison 2025-11-04 17:36:16 -08:00
commit 0e2d9ccd99

View file

@ -50,6 +50,17 @@ except ImportError:
pass pass
def run_sync(coro_func):
"""Run an async callable to completion on a fresh event loop."""
loop = asyncio.new_event_loop()
try:
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro_func())
finally:
asyncio.set_event_loop(None)
loop.close()
class Config(click.ParamType): class Config(click.ParamType):
# This will be removed in Datasette 1.0 in favour of class Setting # This will be removed in Datasette 1.0 in favour of class Setting
name = "config" name = "config"
@ -182,8 +193,7 @@ def inspect(files, inspect_file, sqlite_extensions):
operations against immutable database files. operations against immutable database files.
""" """
app = Datasette([], immutables=files, sqlite_extensions=sqlite_extensions) app = Datasette([], immutables=files, sqlite_extensions=sqlite_extensions)
loop = asyncio.get_event_loop() inspect_data = run_sync(lambda: inspect_(files, sqlite_extensions))
inspect_data = loop.run_until_complete(inspect_(files, sqlite_extensions))
if inspect_file == "-": if inspect_file == "-":
sys.stdout.write(json.dumps(inspect_data, indent=2)) sys.stdout.write(json.dumps(inspect_data, indent=2))
else: else:
@ -610,10 +620,10 @@ def serve(
return ds return ds
# Run the "startup" plugin hooks # Run the "startup" plugin hooks
asyncio.get_event_loop().run_until_complete(ds.invoke_startup()) run_sync(ds.invoke_startup)
# Run async soundness checks - but only if we're not under pytest # Run async soundness checks - but only if we're not under pytest
asyncio.get_event_loop().run_until_complete(check_databases(ds)) run_sync(lambda: check_databases(ds))
if get: if get:
client = TestClient(ds) client = TestClient(ds)
@ -633,9 +643,7 @@ def serve(
if open_browser: if open_browser:
if url is None: if url is None:
# Figure out most convenient URL - to table, database or homepage # Figure out most convenient URL - to table, database or homepage
path = asyncio.get_event_loop().run_until_complete( path = run_sync(lambda: initial_path_for_datasette(ds))
initial_path_for_datasette(ds)
)
url = f"http://{host}:{port}{path}" url = f"http://{host}:{port}{path}"
webbrowser.open(url) webbrowser.open(url)
uvicorn_kwargs = dict( uvicorn_kwargs = dict(