« Head, meet wall - Do not use a mutable as a default parameter in python | Main | Demo of Enthought's TraitsUI with matplotlib and a popup menu »

Demo of launching a traitsui GUI as a separate process and then communicating with it via STDIN/STDOUT

Without going into the details of why, I want to be able to launch a traitsui gui from a python command line and communicate back-and-forth with it while maintaining use of the command line. I played with implementing this with threads in a couple of ways, but those were unsatisfactory as the command line would sometimes go unresponsive for seconds at a time.

Then I had the realization to just launch the traitsui GUI as a separate process and communicate with it via stdin and stdout. So, here's a demo that implements just a few commands and the simplest possible GUI, just a status field in a window. The full code is below.

The only real trick was to include a thread watching for the PID of the original python session to disappear. Otherwise the GUI is left hanging there when the python session is exited.

As an example session in python:

In [1]: from talk2subprocess_traitsui import talker
In [2]: t = talker()  # gui launches
In [3]: t.send_yes()  # gui status window shows the 'yes' message was received
In [4]: t.send_question()  # the question is answered and we get it returned to us
the answer is 83
Out[4]: 83
In [5]: t.shutdown()  # gui closes as intended
In [6]: t = talker()  # gui re-launches
In [7]: quit   # a few seconds later gui disposes of itself

Here's the demo: