Python Gtk2和Vte等待进程Pid完成

3
我正在一个GTK窗口中使用VTE模块来运行和显示bash脚本的结果。
      adresse = self.champ.get_text()    
      pid = self.v.fork_command(None, ['/bin/bash', "./pluzz.sh", adresse])

      if pid == None:         #pseudocode
          print "Finish"

如果子进程中有time.sleep或者循环,那么子进程就会阻塞(不运行)。 我该怎么办?谢谢。

编辑: 尝试了以下方法:

def check_pid(pid):        
    """ Check For the existence of a unix pid. """
    try:
        os.kill(pid, 0)
    except OSError:
        return False
    else:
        return True

问题在于返回值只有一次True,如果我写一个循环,bash脚本会被阻塞。

当您在if语句中检查它时,您希望pid是什么?抱歉我不是很了解这个工具包,我只是想帮忙... - sshashank124
我已经编辑了我的问题,但我不知道这是否是正确的方法。像Xterm一样,在Gtk窗口中分叉一个终端,但集成的。 - Guillaume
我在这里阅读了https://dev59.com/-mgv5IYBdhLWcg3wVPPU,但我不知道是否相同。我不理解这段代码... - Guillaume
也许可以看看这个链接:https://docs.python.org/2/library/os.html#os.waitpid。很抱歉我帮不上太多忙。 - sshashank124
好的,谢谢,我会阅读它。 - Guillaume
我看过这个链接https://dev59.com/eWsz5IYBdhLWcg3wy7HU,但我不能使用它。 - Guillaume
1个回答

1
我找到了一个解决方案:

def __init__(self):
   a=0
   self.fenetre = gtk.Window(gtk.WINDOW_TOPLEVEL)
   [...]
   self.v = vte.Terminal()
#  self.v.connect ("child-exited", lambda term: gtk.main_quit())  # this is the line to change
   self.v.connect ("child-exited", lambda term: self.copie(self, a)) # the redirection after the process is finish
   self.v.show()

def download(self, a, donnees=None): 
   child_pid = self.v.fork_command(None, ['/bin/bash', "./pluzz.sh", adresse])

def copie(self, a, donnees=None):        
              print "FINISH"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接