Python 子进程:知道命令何时完成

Python subprocess: Know when a command has finished

我需要一种方法来知道 ffmpeg 何时完成其工作。这是我的 code:

def on_button_clicked(self, button, progress_bar, filename):
  self.execute(progress_bar, filename)
  progress_bar.set_text('Encoding')
  progress_bar.pulse()

def execute(self, progress_bar, filename):

  cmd = ['ffmpeg', '-y',
         '-i', filename,
         '-r', '30',
         '/tmp/test-encode.mkv']

  process = sp.Popen(cmd, stdin=open(os.devnull))

  progress_bar.set_text('Done')

'Done' 永远不会出现。不过,工作已经完成。我可以在 shell window 中看到 ffmpeg 已完成。我怎样才能收到信号?

尝试强制 GTK 处理 set_text 之后的未决事件:

##  force the refresh of the screen
        while gtk.events_pending():
            gtk.main_iteration()

这是我最终的做法:

    p = sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)

    def mycallback(p):
        p.poll()
        if p.returncode is None:
            # Waiting for Godot
            return True
        else:
            # Yay, wait is over!
            return False

    GObject.timeout_add(1000, mycallback, p)