两个 gnome 终端会话之间的通信 [英] Communication between two gnome-terminal sessions

查看:38
本文介绍了两个 gnome 终端会话之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 python 程序 main.py

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')

handler.py 在哪里

where handler.py is

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

程序 main.py 打开一个新的 gnome 终端并运行 handler.py 以显示In handler...".如何让 handler.py 接收并打印从 main.py 发送的文本发送到处理程序进行显示"?

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?

为问题提供的答案在python脚本之间发送字符串" 是我所追求的想法,handler.py 在 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.

推荐答案

你可以改变你的处理程序来读取命令行给出的文件而不是 stdin:

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)

然后你可以在main.py中创建一个命名管道来发送数据:

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()

这篇关于两个 gnome 终端会话之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆