Allow user to supply loop to App.run, ensure it closes

This commit is contained in:
Darren Burns
2022-01-27 16:38:51 +00:00
parent 092dd569f4
commit 99b6b85d05

View File

@@ -1,9 +1,9 @@
from __future__ import annotations
import os
import asyncio
import os
import warnings
from asyncio import AbstractEventLoop
from typing import Any, Callable, Iterable, Type, TypeVar
import rich.repr
@@ -29,11 +29,10 @@ from .dom import DOMNode
from .driver import Driver
from .file_monitor import FileMonitor
from .geometry import Offset, Region, Size
from .layouts.dock import DockLayout, Dock
from .layouts.dock import Dock
from .message_pump import MessagePump
from .reactive import Reactive
from .view import View
from .views import DockView
from .widget import Widget
# asyncio will warn against resources not being cleared
@@ -43,13 +42,6 @@ LayoutDefinition = "dict[str, Any]"
ViewType = TypeVar("ViewType", bound=View)
try:
import uvloop
except ImportError:
pass
else:
uvloop.install()
class AppError(Exception):
pass
@@ -200,6 +192,7 @@ class App(DOMNode):
console: Console = None,
screen: bool = True,
driver: Type[Driver] = None,
loop: AbstractEventLoop = None,
**kwargs,
):
"""Run the app.
@@ -208,13 +201,28 @@ class App(DOMNode):
console (Console, optional): Console object. Defaults to None.
screen (bool, optional): Enable application mode. Defaults to True.
driver (Type[Driver], optional): Driver class or None for default. Defaults to None.
loop (): Event loop to run the application on. If not specified, uvloop will be used.
"""
async def run_app() -> None:
app = cls(console=console, screen=screen, driver_class=driver, **kwargs)
await app.process_messages()
asyncio.run(run_app())
if loop:
asyncio.set_event_loop(loop)
else:
try:
import uvloop
except ImportError:
pass
else:
uvloop.install()
event_loop = asyncio.get_event_loop()
try:
asyncio.run(run_app())
finally:
event_loop.close()
async def _on_css_change(self) -> None: