Skip to content

run as background process, connect to active terminal session #1087

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

Open
tcrensink opened this issue Mar 1, 2020 · 4 comments
Open

run as background process, connect to active terminal session #1087

tcrensink opened this issue Mar 1, 2020 · 4 comments

Comments

@tcrensink
Copy link

I want to create an TUI that runs in the background but connects to an active terminal session on command. Is this possible?

I have an app that uses prompt_toolkit, that runs with python app.py; after (slow) initial startup it is quite responsive. Is is possible to keep it running in the background and connect to the tty of a terminal session, to avoid the delay of quitting/restarting the process?

@jonathanslenders
Copy link
Member

Hi @tcrensink,

I assume you are looking for a way to detach the process from the TTY, like tmux, screen and others.
You can have a look at pymux, which does it, but honestly the code is not reusable, and pretty complex. (And I'm not 100% sure how stable it is on the latest prompt_toolkit version.)

The idea is to provide a custom Output class that you pass to the prompt_toolkit Application. This shouldn't write to stdout, but to a pipe. The the actual program running in the TTY is one that connects to the prompt_toolkit application over a unix socket. The render output is passed over the socket and written to the TTY. The server side of this piece will actually listen on the unix socket, and create new prompt_toolkit Application classes for each incoming connection. There's probably a bit more to take into account, like handling terminal resize events, double forking the server the first time the client starts, the implementation of the Input class and so on.

@tcrensink
Copy link
Author

tcrensink commented Mar 4, 2020

@jonathanslenders thank you for the thorough response. You are correct: I'd like to mimic the behavior of tmux/screen for fast attaching/detaching to a running prompt-toolkit process.

For clarity: are you suggesting that this could be implemented as it is done in pymux using prompt-toolkit only (and of course required os resources)? Any links in pymux for where the process you describe above happens would be helpful.

@karandwivedi42
Copy link

I'd like to mimic the behavior of tmux/screen for fast attaching/detaching to a running prompt-toolkit process.

@tcrensink did you find a solution for this?

@tcrensink
Copy link
Author

@karandwivedi42, I found a hack that works ok. I ran the prompt_toolkit process in a Docker container, then connect/disconnect to the container with keybindings, while leaving the prompt_toolkit process running. The command to conntect/disconnect (and then clear the terminal window) looks something like this:

docker attach my_prompt_toolkit_container --detach-keys="ctrl-c"; printf "\033c"

I renamed the above command my_app for convenience; this is not what I would call a production quality solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants