문제

I have python program main.py

import subprocess

p = subprocess.Popen(
  "/usr/bin/gnome-terminal -x 'handler.py'", 
  shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
p.stdin.write('Text sent to handler for display\n')

where handler.py is

#!/usr/bin/python
print "In handler..."

Program main.py opens a new gnome-terminal and runs handler.py to display "In handler...". How can I get handler.py to receive and print "Text sent to the handler for display" sent from main.py?

The answer provided to question "Sending strings between python scripts" is the idea of what I'm after, where handler.py runs in the terminal session created by main.py.

도움이 되었습니까?

해결책

You could change your handler to read from a file given at the command line instead of stdin:

#!/usr/bin/env python
import shutil
import sys
import time

print "In handler..."
with open(sys.argv[1], 'rb') as file:
    shutil.copyfileobj(file, sys.stdout)
sys.stdout.flush()
time.sleep(5)

Then you could create a named pipe in main.py, to send the data:

#!/usr/bin/env python
import os
from subprocess import Popen

fifo = "fifo"
os.mkfifo(fifo)
p = Popen(["gnome-terminal", "-x", "python", "handler.py", fifo])
with open(fifo, 'wb') as file:
    file.write("Text sent to handler for display")
os.remove(fifo)
p.wait()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top