【发布时间】:2014-07-18 08:03:42
【问题描述】:
我有一个 pyqt gui 并调用一个长进程 (ffmpeg),我把它放在一个单独的线程上以不阻塞 gui。然后,我想在更长的命令列表中的一个命令完成时更新进度条。问题是,我无法从工作线程中调用 gui 线程中的函数。所以我让在工作线程中运行一个ticker,但是当我用while循环更新进度条并读取ticker值时,gui再次被阻塞。我该如何解决这个问题。我目前使用的是 python 线程而不是 Qthread。 感谢您的帮助!
import threading, pexpect
self.cmd_list = ['ffmpeg -i file outfile','and so on']
self.stop_proc = False
self.executeCMD()
def spawn_ffmpeg_cmd(self):
for cmd in self.cmd_list:
if self.stop_proc == False:
thread = pexpect.spawn(cmd)
print "\nstarted: %s" % cmd
cpl = thread.compile_pattern_list([pexpect.EOF,"frame= *\d+ fps=*\d+",'(.+)'])
while True:
i = thread.expect_list(cpl, timeout=None)
if i == 0: # EOF
print "the sub process exited"
self.pgticker += 1
break
elif i == 1:
frame_number_fps = thread.match.group(0)
print frame_number_fps
thread.close
elif i == 2:
pass
self.startButton.setEnabled(True)
def executeCMD(self):
self.startButton.setEnabled(False)
self.pgticker = 0
threading.Thread(target=self.spawn_ffmpeg_cmd, name="_proc").start()
def stopprocess(self):
self.stop_proc = True
self.cmd_list = []
os.system('pkill ffmpeg')
self.pgticker = len(self.cmd_list)
self.startButton.setEnabled(True)
def updateProgress(self):
pgfactor = 100 / len(self.cmd_list)
progress = 0.0
progress = pgfactor*int(self.pgticker)
self.progressBar.setProperty("value", progress)
【问题讨论】:
标签: python multithreading user-interface ffmpeg