Skip to content

Commit

Permalink
Try out cancelling a root cancel scope for guest mode teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Nov 4, 2021
1 parent 186d221 commit ffde4db
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions piker/ui/_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from qdarkstyle import DarkPalette
# import qdarkgraystyle
import trio
from outcome import Error
from outcome import Error, Outcome

from .._daemon import maybe_open_pikerd, _tractor_kwargs
from ..log import get_logger
Expand Down Expand Up @@ -73,12 +73,16 @@


def run_qtractor(

func: Callable,
args: Tuple,
main_widget: QtGui.QWidget,

tractor_kwargs: Dict[str, Any] = {},
window_type: QtGui.QMainWindow = None,
) -> None:

) -> int:

# avoids annoying message when entering debugger from qt loop
pyqtRemoveInputHook()

Expand Down Expand Up @@ -119,7 +123,7 @@ def run_sync_soon_threadsafe(fn):
event.fn = fn
app.postEvent(reenter, event)

def done_callback(outcome):
def done_callback(outcome: Outcome) -> Outcome:

if isinstance(outcome, Error):
exc = outcome.error
Expand All @@ -131,8 +135,11 @@ def done_callback(outcome):
else:
traceback.print_exception(type(exc), exc, exc.__traceback__)

# tear down Qt when ``trio`` completes
app.quit()

return outcome

# load dark theme
stylesheet = qdarkstyle.load_stylesheet(
qt_api='pyqt5',
Expand Down Expand Up @@ -164,13 +171,25 @@ def done_callback(outcome):
# override tractor's defaults
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
async def main():

async with maybe_open_pikerd(
**tractor_kwargs,
):
await func(*((instance,) + args))
nonlocal root_trio_cs

with root_trio_cs as rcs:

async with maybe_open_pikerd(
**tractor_kwargs,
):
return await func(*((instance,) + args))

if rcs.cancelled_caught:
print('Terminated')
return

# guest mode entry
trio.lowlevel.start_guest_run(
Expand All @@ -185,4 +204,6 @@ async def main():

# actually render to screen
window.show()
app.exec_()

return_code: int = app.exec_()
return return_code

0 comments on commit ffde4db

Please sign in to comment.