【发布时间】:2019-11-29 05:26:55
【问题描述】:
我有一个执行以下操作的 python 函数:
- 启动一个新线程,并在其中运行 PyQt QApplication。
- 工作 N 秒,并将进度发送到运行 PyQt 的线程。
现在,在我启动 PyQt QApplication 之前,主线程中的 time.sleep() 按预期工作,但在我启动它之后,time.sleep(1) 立即返回。
这是一个例子:
def run_gui_thread(self):
app = QApplication([])
self.prompt = QDialog()
self.ready = True
app.exec_()
还有:
def do_work(self):
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
self.thread = threading.Thread(target = self.run_gui_thread)
self.thread.start()
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
time.sleep(1)
print "Now is %s." % time.strftime("%H:%M:%S")
输出:
Now is 10:00:56.
Now is 10:00:57.
Now is 10:00:58.
Now is 10:00:59.
Now is 10:00:59.
Now is 10:00:59.
Now is 10:00:59.
Now is 10:00:59.
有什么建议吗?
提前致谢。
【问题讨论】:
-
作为一种快速破解方法,您可以尝试
import select; select.select([], [], [], 1),看看它是否能更好地承受信号。
标签: python multithreading pyqt