|
150 | 150 | if constants.DEBUG: |
151 | 151 | warnings.simplefilter("always", ResourceWarning) |
152 | 152 |
|
| 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 | + |
153 | 156 | ComposeResult = Iterable[Widget] |
154 | 157 | RenderResult: TypeAlias = "RenderableType | Visual | SupportsVisual" |
155 | 158 | """Result of Widget.render()""" |
@@ -2140,20 +2143,24 @@ def run( |
2140 | 2143 | App return value. |
2141 | 2144 | """ |
2142 | 2145 |
|
2143 | | - async def run_app() -> None: |
| 2146 | + async def run_app() -> ReturnType | None: |
2144 | 2147 | """Run the app.""" |
2145 | | - await self.run_async( |
| 2148 | + return await self.run_async( |
2146 | 2149 | headless=headless, |
2147 | 2150 | inline=inline, |
2148 | 2151 | inline_no_clear=inline_no_clear, |
2149 | 2152 | mouse=mouse, |
2150 | 2153 | size=size, |
2151 | 2154 | auto_pilot=auto_pilot, |
2152 | 2155 | ) |
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()) |
2157 | 2164 |
|
2158 | 2165 | async def _on_css_change(self) -> None: |
2159 | 2166 | """Callback for the file monitor, called when CSS files change.""" |
|
0 commit comments