Try out cancelling a root cancel scope for guest mode teardown

teardown_guesmost_via_cs
Tyler Goodlet 2021-08-03 09:42:32 -04:00
parent 186d221dda
commit ffde4db656
1 changed files with 29 additions and 8 deletions

View File

@ -39,7 +39,7 @@ import qdarkstyle
from qdarkstyle import DarkPalette from qdarkstyle import DarkPalette
# import qdarkgraystyle # import qdarkgraystyle
import trio import trio
from outcome import Error from outcome import Error, Outcome
from .._daemon import maybe_open_pikerd, _tractor_kwargs from .._daemon import maybe_open_pikerd, _tractor_kwargs
from ..log import get_logger from ..log import get_logger
@ -73,12 +73,16 @@ if platform.system() == "Windows":
def run_qtractor( def run_qtractor(
func: Callable, func: Callable,
args: Tuple, args: Tuple,
main_widget: QtGui.QWidget, main_widget: QtGui.QWidget,
tractor_kwargs: Dict[str, Any] = {}, tractor_kwargs: Dict[str, Any] = {},
window_type: QtGui.QMainWindow = None, window_type: QtGui.QMainWindow = None,
) -> None:
) -> int:
# avoids annoying message when entering debugger from qt loop # avoids annoying message when entering debugger from qt loop
pyqtRemoveInputHook() pyqtRemoveInputHook()
@ -119,7 +123,7 @@ def run_qtractor(
event.fn = fn event.fn = fn
app.postEvent(reenter, event) app.postEvent(reenter, event)
def done_callback(outcome): def done_callback(outcome: Outcome) -> Outcome:
if isinstance(outcome, Error): if isinstance(outcome, Error):
exc = outcome.error exc = outcome.error
@ -131,8 +135,11 @@ def run_qtractor(
else: else:
traceback.print_exception(type(exc), exc, exc.__traceback__) traceback.print_exception(type(exc), exc, exc.__traceback__)
# tear down Qt when ``trio`` completes
app.quit() app.quit()
return outcome
# load dark theme # load dark theme
stylesheet = qdarkstyle.load_stylesheet( stylesheet = qdarkstyle.load_stylesheet(
qt_api='pyqt5', qt_api='pyqt5',
@ -164,13 +171,25 @@ def run_qtractor(
# override tractor's defaults # override tractor's defaults
tractor_kwargs.update(_tractor_kwargs) tractor_kwargs.update(_tractor_kwargs)
# setup a root scope to be cancelled on Qt exit
root_trio_cs = trio.CancelScope()
app.lastWindowClosed.connect(root_trio_cs.cancel)
# define tractor entrypoint # define tractor entrypoint
async def main(): async def main():
async with maybe_open_pikerd( nonlocal root_trio_cs
**tractor_kwargs,
): with root_trio_cs as rcs:
await func(*((instance,) + args))
async with maybe_open_pikerd(
**tractor_kwargs,
):
return await func(*((instance,) + args))
if rcs.cancelled_caught:
print('Terminated')
return
# guest mode entry # guest mode entry
trio.lowlevel.start_guest_run( trio.lowlevel.start_guest_run(
@ -185,4 +204,6 @@ def run_qtractor(
# actually render to screen # actually render to screen
window.show() window.show()
app.exec_()
return_code: int = app.exec_()
return return_code