How to fork a python script in different terminal
<p>I want to fork a new process in a script, but how to interactive with the subprocess in a new terminal?
For example:</p>

<pre><code>#python
a='a'
b='b'
if os.fork():
    print a
    a = input('a?')
    print 'a:',a
else:
    print b
    b = input('b?')
    print 'b:',b
</code></pre>

<p>The script should print a/b and ask for a new value. But these two process share a same terminal, and that makes it confused.</p>

<p>How can I open a new terminal and let the subprocess run in the new terminal?</p>

<hr>

<p>I've thought about to use <code>subprocess.Popen('gnome-terminal',shell=True)</code> and communicate with the new terminal. But <code>gnome-terminal</code> will open bash on default, how can i open a terminal only for input and output?</p>

<h2>I finally implement it in a(maybe ugly) way.</h2>

<p>Inspired by <a href="http://unix.stackexchange.com/questions/256480/how-do-i-run-a-command-in-a-new-terminal-window-in-the-same-process-as-the-origi">http://unix.stackexchange.com/questions/256480/how-do-i-run-a-command-in-a-new-terminal-window-in-the-same-process-as-the-origi</a>
I'v solve most of the problem:</p>

<pre><code>#python
import sys,os,subprocess
a='a'
b='b'
if os.fork():
    print a
    a = raw_input('a?')
    print 'a:',a
else:
    p = subprocess.Popen("xterm -e 'tty &gt;&amp;3; exec sleep 99999999' 3&gt;&amp;1",
                     shell=True, stdout=subprocess.PIPE,   stdin=subprocess.PIPE)
    tty_path = p.stdout.readline().strip()
    tty = open(tty_path,'r+')
    sys.stdout=tty
    sys.stderr=tty
    sys.stdin=tty
    print b
    b = raw_input('b?')
    print 'b:',b
</code></pre>

<p>The only problem is that the prompt: 'b?' will still show in the former terminal. So the new question is: where does prompt belongs?</p>

<hr>

<p>Despite that, another way to solve this prompt problem:</p>

<pre><code>_r_i = raw_input
def raw_input(prompt):
    print prompt,
    return _r_i('')
</code></pre>

<hr>

<p>I'm a little strange and, mad... I know...</p>

python linux bash shell subprocess