gobject和子流程.Popen在GTK图形用户界面中进行通信

2024-04-24 00:17:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用gobject来允许Popen进程和gtkgui之间的通信。在

受此启发: https://pygabriel.wordpress.com/2009/07/27/redirecting-the-stdout-on-a-gtk-textview/#comment-156

我实施了类似的方法:

http://hartree.altervista.org/files/command-textview.py

但是我注意到即使Popen进程被终止,gobject也会占用大量的CPU周期。只需运行上面的脚本并查看Ubuntu系统监视器。在

在与“pty”合作之后,我想到了这个:

import gtk,pygtk
import subprocess
import gobject
import pty, os, time

class CommandTextView(gtk.TextView):
    def __init__(self):
        super(CommandTextView,self).__init__()
        self.master, self.slave = pty.openpty()
        gobject.io_add_watch(os.fdopen(self.master), gobject.IO_IN, self.write_to_buffer)
        self.proc = None

    def run(self, w, cmd):
        if self.proc == None or self.proc.poll() != None: # poll()=None means still running
            self.proc = subprocess.Popen(cmd.split(), shell=True, stdout=self.slave, stderr=self.slave)

    def stop(self,w):
        if type(self.proc) is subprocess.Popen:
            self.proc.kill()
            while self.proc.poll() == None:
                time.sleep(0.1)
            self.proc = None

    def write_to_buffer(self, fd, condition):
        if condition == gobject.IO_IN:
            char = fd.readline()
            print 'adding:',char    
            buf = self.get_buffer()
            buf.insert_at_cursor(char)
            return True
        else:
            return False

def test():
    win=gtk.Window()
    vbox = gtk.VBox(False, 0)
    win.set_size_request(300,300)
    win.connect('delete-event',lambda w,e : gtk.main_quit())
    ctv=CommandTextView()
    bt1 = gtk.Button('Run')
    bt2 = gtk.Button('Stop')
    vbox.pack_start(ctv)
    vbox.pack_end(bt2,False,False)
    vbox.pack_end(bt1,False,False)
    win.add(vbox)

    bt1.connect("clicked", ctv.run, 'ls -la')
    bt2.connect("clicked", ctv.stop)
    win.show_all()
    gtk.main()

if __name__=='__main__': test()

我的问题是:

  • pty是个好主意吗?它也可以用于Windows吗?

  • 是否可以避免使用pty而只使用stdout而不存在CPU使用率高的问题?

  • 如果您第一次运行这个脚本,它似乎会缓冲txt输出并给出一个不完整的输出。

谢谢你的帮助


Tags: importselfnonefalsegtkifdefstdout
2条回答

os.read使用无缓冲读取,它需要一个实际的文件描述符。fd不是一个真正的文件描述符,它是一个文件对象;通常称为f

如果要确保进程已停止,请使用os.杀死. 在

这是给那些在2016年之后无意中看到这篇文章并试图将其改写为Gtk3的人。在

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk
from gi.repository import GObject

import os
import fcntl
import subprocess

def unblock_fd(stream):
    fd = stream.fileno()
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)


class StreamTextBuffer(Gtk.TextBuffer):
    '''TextBuffer read command output syncronously'''
    def __init__(self):
        Gtk.TextBuffer.__init__(self)
        self.IO_WATCH_ID = tuple()


    def bind_subprocess(self, proc):
        unblock_fd(proc.stdout)
        watch_id_stdout = GObject.io_add_watch(
            channel   = proc.stdout,
            priority_ = GObject.IO_IN,
            condition = self.buffer_update,
            # func      = lambda *a: print("func") # when the condition is satisfied
            # user_data = # user data to pass to func
        )

        unblock_fd(proc.stderr)
        watch_id_stderr = GObject.io_add_watch(
            channel   = proc.stderr,
            priority_ = GObject.IO_IN,
            condition = self.buffer_update,
            # func      = lambda *a: print("func") # when the condition is satisfied
            # user_data = # user data to pass to func
        )

        self.IO_WATCH_ID = (watch_id_stdout, watch_id_stderr)
        return self.IO_WATCH_ID


    def buffer_update(self, stream, condition):
        self.insert_at_cursor(stream.read())
        return True # otherwise isn't recalled


def sample():
    root = Gtk.Window()
    root.set_default_size(400, 260)
    root.connect("destroy", Gtk.main_quit)
    root.connect( # quit when Esc is pressed
        'key_release_event',
        lambda w, e: Gtk.main_quit() if e.keyval == 65307 else None
    )
    layout = Gtk.Box(orientation=1)
    scroll = Gtk.ScrolledWindow()
    layout.pack_start(scroll, expand=1, fill=1, padding=0)

    buff = StreamTextBuffer()
    textview = Gtk.TextView.new_with_buffer(buff)
    scroll.add(textview)

    button_start = Gtk.Button("Execute Command")
    layout.add(button_start)

    def on_click(widget):
        if len(buff.IO_WATCH_ID):
            for id_ in buff.IO_WATCH_ID:
                # remove subprocess io_watch if not removed will
                # creates lots of cpu cycles, when process dies
                GObject.source_remove(id_)
            buff.IO_WATCH_ID = tuple()
            on_click.proc.terminate() # send SIGTERM
            widget.set_label("Execute Command")
            return

        on_click.proc = subprocess.Popen(
            [ 'ping', '-c', '3', 'localhost' ],
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE,
            universal_newlines=True,
        )
        buff.bind_subprocess(on_click.proc)
        widget.set_label("STOP!")

    button_start.connect("clicked", on_click)
    root.add(layout)
    root.show_all()


if __name__ == "__main__":
    sample()
    Gtk.main()

相关问题 更多 >