簡體   English   中英

如何使gnome-terminal命令像python中的對象一樣工作

[英]How to make gnome-terminal command act like object in python

我試圖創建一個gnome終端窗口,然后在終端窗口(回聲)中編寫一些內容,例如while循環中的緩沖屏幕。

 import os x = os.system("gnome-terminal -e") while True: x.write("echo % s "%(buffering)) if progress == 0: break 

因此,我必須在while循環之前打開gnome-terminal窗口,因為如果不這樣做,gnome終端將在每個循環中打開和關閉。

您可以使用命名管道:

import os,time

def client():
    try:
        os.mkfifo("named_pipe")
    except OSError as e:
        print(e.message)
    named_pipe = os.open("named_pipe", os.O_WRONLY)
    while True:
        time.sleep(1)
         os.write(named_pipe, 'Time {}\n'.format(time.asctime()))


def server():
    from subprocess import check_call
check_call(['gnome-terminal', '-e', "python script.py"])


pid = os.fork()
foo() if pid != 0 else bar()

在script.py中:

import os
with open('named_pipe') as f:
    for line in iter(f.readline, ""):
        print(line.rstrip())
    os.unlink("named_pipe")

您也可以使用Unix域套接字或TCP。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM