Skip to content

Teardown guestmode via "root" cs around tractor.open_root_actor() #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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