使用xterm打开新控制台:如何在当前控制台进行打印的同时在新控制台上进行打印 [英] Using xterm to open a new console: How to while the current console is printing, to print on the new console also

查看:161
本文介绍了使用xterm打开新控制台:如何在当前控制台进行打印的同时在新控制台上进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用python.我有一个代表我整个程序的线程.我想使用os.system(xterm&)作为有效的线程来打开另一个控制台窗口.唯一的事情是,当另一个线程正在打印到较旧的窗口时,是否可以打印到新窗口?

I'm using python right now. I have a thread that represents my entire program. I want to open another console window using os.system(xterm&) as a thread which works. The only thing is, is it possible to print to the new window while the other thread is printing to the older window?

import sys
import os

def my_fork():
    child_pid = os.fork()
    if child_pid == 0:

        function=open('SMITH747.txt','r')
        f=function.readlines()
        fd = open("output", "w")
        # Open xterm window with logs from that file
        p = subprocess.Popen(["xterm", "-e", "tail", "-f", "output"])
        # Do logging


        while(True):
            for i in range(1):
                fd.write("Hello, world!")
                time.sleep(2)

            if f==[]:
                pass
            else:
                bud=False

            fd.flush()

    else:
        function=open('SMITH747.txt','w')
        var=input('Enter ')
        if var=='q':
            function.write('yo')

if __name__ == "__main__":
    my_fork()

这就是我现在所拥有的:它可以正常工作,但是如果f不是[],我将无法读取它并终止文件.如果有人可以帮助我调试此部分,我将非常感激.那就完美了!

this is what I have right now: It works except I can't get it to read my file and terminate if f is not []. I would appreciate it so much if someone can help me debug this part. Then it will be perfect!

推荐答案

import os, subprocess, time, threading

# Reads commands from fifo in separate thread.
# if kill command is received, then down continue flag.
class Killer(threading.Thread):
   def __init__ (self):
        threading.Thread.__init__(self)
        self.continueFlag = True
   def run(self):
        fd=open('ipc','r')
        command = fd.read()
        if command == "kill\n":
            self.continueFlag = False

def my_fork():

    # create fifo for reliable inter-process communications
    # TODO: check existence of fifo
    os.mkfifo('ipc')

    # Be careful with threads and fork
    child_pid = os.fork()
    if child_pid == 0:

        fd = open("output", "w")
        subprocess.Popen(["xterm", "-e", "tail", "-f", "output"])

        # Create and start killer, be careful with threads and fork
        killer = Killer()
        killer.start()

        # Perform work while continue flag is up
        while(killer.continueFlag):
            fd.write("Hello, world!\n")
            fd.flush()
            time.sleep(2)

        # need to kill subprocess with opened xterm

    else:
        # File must be fifo, otherwise race condition is possible.
        fd=open('ipc','w')
        var=input('Enter ')
        if var=='q':
            fd.write('kill\n')

if __name__ == "__main__":
    my_fork()

P.S.讨论离主题很远,也许您应该更改它.

P.S. discussion is far away from topic, probably you should change it.

这篇关于使用xterm打开新控制台:如何在当前控制台进行打印的同时在新控制台上进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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