Skip to content

Commit b9afd28

Browse files
committed
don't call get_event_loop() if it's deprecated
1 parent 8918d31 commit b9afd28

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/textual/app.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
if constants.DEBUG:
151151
warnings.simplefilter("always", ResourceWarning)
152152

153+
# `asyncio.get_event_loop()` is deprecated since Python 3.10:
154+
_ASYNCIO_GET_EVENT_LOOP_IS_DEPRECATED = sys.version_info >= (3, 10, 0)
155+
153156
ComposeResult = Iterable[Widget]
154157
RenderResult: TypeAlias = "RenderableType | Visual | SupportsVisual"
155158
"""Result of Widget.render()"""
@@ -2140,20 +2143,24 @@ def run(
21402143
App return value.
21412144
"""
21422145

2143-
async def run_app() -> None:
2146+
async def run_app() -> ReturnType | None:
21442147
"""Run the app."""
2145-
await self.run_async(
2148+
return await self.run_async(
21462149
headless=headless,
21472150
inline=inline,
21482151
inline_no_clear=inline_no_clear,
21492152
mouse=mouse,
21502153
size=size,
21512154
auto_pilot=auto_pilot,
21522155
)
2153-
2154-
event_loop = asyncio.get_event_loop() if loop is None else loop
2155-
event_loop.run_until_complete(run_app())
2156-
return self.return_value
2156+
if loop is None:
2157+
if _ASYNCIO_GET_EVENT_LOOP_IS_DEPRECATED:
2158+
# N.B. This does work with Python<3.10, but global Locks, Events, etc
2159+
# eagerly bind the event loop, and result in Future bound to wrong
2160+
# loop errors.
2161+
return asyncio.run(run_app())
2162+
return asyncio.get_event_loop().run_until_complete(run_app())
2163+
return loop.run_until_complete(run_app())
21572164

21582165
async def _on_css_change(self) -> None:
21592166
"""Callback for the file monitor, called when CSS files change."""

0 commit comments

Comments
 (0)