I am trying to output different information in two terminals from the same Python script (much like this fellow). The way that my research has seemed to point to is to open a new xterm window using subprocess.Popen and running cat to display the stdin of the terminal in the window. I would then write the necessary information to the stdin of the subprocess like so:
from subprocess import Popen, PIPE
terminal = Popen(['xterm', '-e', 'cat'], stdin=PIPE) #Or cat > /dev开发者_C百科/null
terminal.stdin.write("Information".encode())
The string "Information" would then display in the new xterm. However, this is not the case. The xterm does not display anything and the stdin.write method simply returns the length of the string and then moves on. I'm not sure is there is a misunderstanding of the way that subprocess and pipes work, but if anyone could help me it would be much appreciated. Thanks.
This doesn't work because you pipe stuff to xterm
itself not the program running inside xterm
. Consider using named pipes:
import os
from subprocess import Popen, PIPE
import time
PIPE_PATH = "/tmp/my_pipe"
if not os.path.exists(PIPE_PATH):
os.mkfifo(PIPE_PATH)
Popen(['xterm', '-e', 'tail -f %s' % PIPE_PATH])
for _ in range(5):
with open(PIPE_PATH, "w") as p:
p.write("Hello world!\n")
time.sleep(1)
精彩评论