3

I need output different information to different terminal instances instead of print them in same output stream, say std.err or std.out.

for example: I have 5 kinds of information say A-E need to be displayed on different terminal windows on same desktop, looks like

[terminal 1] <- for displaying information A

[terminal 2] <- for displaying information B

[terminal 3] <- for displaying information C

[terminal 4] <- for displaying information D

[terminal 5] <- for displaying information E

I know I can output them into different files, then open terminals read the file in loop, but what I want is python program can open terminal by program itself and print to them directly when it is needed.

Is it possible?

Thanks!

KC

[edit] the best solution for this case is using SOCKET as the IPC I think if the resource is not a matter, it will come with best compatible capability - a server client mode. and the pipe / subprocess will also be the useful solutions under same platform

2 Answers 2

3

Open a pipe, then fork off a terminal running cat reading from the read end of the pipe, and write into the write end of the pipe.

Sign up to request clarification or add additional context in comments.

Comments

1

Using the subprocess module, just run several instances of whichever terminal program you like, each running "cat", using subprocess.Popen. Pass stdin=subprocess.PIPE in addition to the terminal command to Popen. Then you can just write to each terminal's stdin attribute.

Something along the lines of (untested!):

import subprocess
p = subprocess.Popen('xterm -e "cat > /dev/null"', stdin=subprocess.PIPE)
p.stdin.write("Hello World!")

2 Comments

thanks! the original line seems missing parameter "shell=True," otherwise it will goes exceptional. however, after I added the shell=True, is shows nothing on the xterm... import subprocess p = subprocess.Popen('xterm -e "cat > /dev/null"', shell=True, stdin=subprocess.PIPE) p.stdin.write("Hello World!")
Perhaps this is because of buffering? Try p.stdin.flush() after writing to it. I'm sorry but I don't have access to a Linux terminal at the moment to try this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.