线程停止执行 Tkinter 程序
Threading halts execution of Tkinter program
嘿,我正在编写一个与 jenkins 一起工作的接口来触发构建作业和部署。我一直坚持的一项功能是能够在构建完成后获取其状态。
截至目前,我有一个使用 Tkinter 实现的 GUI,除了缺少有关最终构建状态的信息外,该应用程序可以正常运行。
我正在尝试轮询 jenkins 以获取信息,但我需要给它时间在轮询之前完成构建。我以为我可以通过一个简单的线程来完成此操作,只需将它 运行 放在后台,但是,当线程为 运行 并且它命中 time.sleep() 函数时,它会停止其余的该程序也是如此。
这是否可以在不停止程序的其余部分(即 GUI)的情况下完成,如果可以,我哪里出错了?
这是问题区域的片段:
def checkBuildStatus(self):
monitor_thread = threading.Thread(target=self._pollBuild())
monitor_thread.daemon = True
monitor_thread.start()
def _pollBuild(self):
# now sleep until the build is done
time.sleep(15)
# get the build info for the last job
build_info = self.server.get_build_info(self.current_job, self.next_build_number)
result = build_info['result']
创建线程时,需要传递函数本身。确保不要调用该函数。
monitor_thread = threading.Thread(target=self._pollBuild())
# ^^
应该是:
monitor_thread = threading.Thread(target=self._pollBuild)
嘿,我正在编写一个与 jenkins 一起工作的接口来触发构建作业和部署。我一直坚持的一项功能是能够在构建完成后获取其状态。
截至目前,我有一个使用 Tkinter 实现的 GUI,除了缺少有关最终构建状态的信息外,该应用程序可以正常运行。
我正在尝试轮询 jenkins 以获取信息,但我需要给它时间在轮询之前完成构建。我以为我可以通过一个简单的线程来完成此操作,只需将它 运行 放在后台,但是,当线程为 运行 并且它命中 time.sleep() 函数时,它会停止其余的该程序也是如此。
这是否可以在不停止程序的其余部分(即 GUI)的情况下完成,如果可以,我哪里出错了?
这是问题区域的片段:
def checkBuildStatus(self):
monitor_thread = threading.Thread(target=self._pollBuild())
monitor_thread.daemon = True
monitor_thread.start()
def _pollBuild(self):
# now sleep until the build is done
time.sleep(15)
# get the build info for the last job
build_info = self.server.get_build_info(self.current_job, self.next_build_number)
result = build_info['result']
创建线程时,需要传递函数本身。确保不要调用该函数。
monitor_thread = threading.Thread(target=self._pollBuild())
# ^^
应该是:
monitor_thread = threading.Thread(target=self._pollBuild)