subprocess.wait

时间:2017-09-20 19:23:21

标签: python subprocess byte stdin wait

我正在使用omxplayer编写一个音乐播放器系统,一切都已完成,但我有问题,我需要检测音乐何时由subprocess.wait()完成,直到进程结束,我必须写入字节到过程如'p'.encode()暂停,'q'.encode()停止等... 问题是我在等待它时不能将字节写入子进程...如果有人知道如何检测进程的结束并同时写入它,那么欢迎你!

这是我的代码:

class MusicPlayer(Thread):
    def __init__(self, manager, playlist=[]):
        self.playlist = []
        self.index = 0
        self.process = None
        self.paused = False
        self.stopped = False
        self.manager = manager
        self.progress_bar = MusicProgressBar(self)
        self.progress_bar.start()
        Thread.__init__(self)
    def run(self):
        while True:
            if len(self.playlist) is 0:
                time.sleep(1)
                continue

            self.process = subprocess.Popen(['omxplayer' , '-o' , 'local', self.playlist[self.index].file_url])
            self.progress_bar.play(self.playlist[0].file_url)
            self.paused = False
            self.process.wait()
            self.index += 1
            if self.index >= len(self.playlist):
                self.index = 0
    def play(self, playlist):
        self.playlist = playlist
        self.index = 0
    def next(self):
        if self.process is None: return
        self.process.stdin.write('q'.encode())
        self.process.stdin.flush()

    def before(self):
        for i in range(0,2):
            self.index -= 1
            if self.index < 0:
                self.index = len(self.playlist-1)

    def stop(self):
        if self.process is None: return
        self.process.stdin.write('q'.encode())
        self.process.stdin.flush()
        self.stopped = True

    def pause(self):
        if self.process is None: return
        if self.paused: return
        self.process.stdin.write('p'.encode())
        self.process.stdin.flush()
        self.paused = True

    def resume(self):
        if self.process is None: return
        if not self.paused: return
        self.process.stdin.write('p'.encode())
        self.process.stdin.flush()
        self.paused = False

非常感谢您的回答! 最好的问候,Julien

1 个答案:

答案 0 :(得分:0)

我认为你可以改变这一行

        self.process.wait()

到一个while循环

while self.process.poll() not None:
  c = stdscr.getch()
  if c != -1:
     # check p, q

这是继续轮询omxplayer过程是否完成。如果没有,请检查是否有按键。