qtimer和qthread的使用逻辑有什么缺陷吗?
我有一个使用 pyqt4 开发的 GUI,它有一个运行按钮。单击运行按钮时,我调用一个计时器和一个线程。计时器持续监视线程。在线程上,我调用命令提示符来执行测试用例。我希望线程在打开命令提示符之前一直处于活动状态,并在关闭命令提示符后将其视为已死线程。
我为实现此目的而编写的代码如下。有什么逻辑缺陷吗?或者有更好的方法来实现这一目标?
self.connect(self.run_button, SIGNAL('clicked()'), self.runscript)
def runscript(self):
self.timer = QTimer()
self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)
self.timer.start(1000)
def sendData(self):
if self.run_timer:
run_monitor_object = RunMonitor()
print 'Starting the thread...........'
run_monitor_object.start()
self.run_timer = False
if run_monitor_object.isAlive():
print 'Thread Alive...'
else:
print 'Thread is Dead....'
class RunMonitor(threading.Thread):
def __init__(self, parent=None):
threading.Thread.__init__(self)
def run(self):
print 'Invoking Command Prompt..........'
subprocess.call(["start", "/DC:\\Scripts", "scripts_to_execute.bat"], shell=True)
当我运行此命令时,出现以下错误...
UnboundLocalError: 在 if run_monitor_object.isAlive() 赋值之前引用局部变量“run_monitor_object”:
只是想知道还有什么办法,我可以
I have a GUI developed using pyqt4 which has a run button. on run button click, I invoke a timer and a thread. the timer keeps monitoring the thread. on the thread I invoke a command prompt to execute the test cases. I want the thread to be alive till the command prompt is opened and want to say it as dead once I close the command prompt.
The code that I had written to achieve this is as below. Any logic flaws? or any better way to achieve this?
self.connect(self.run_button, SIGNAL('clicked()'), self.runscript)
def runscript(self):
self.timer = QTimer()
self.timer.connect(self.timer, SIGNAL("timeout()"),self.sendData)
self.timer.start(1000)
def sendData(self):
if self.run_timer:
run_monitor_object = RunMonitor()
print 'Starting the thread...........'
run_monitor_object.start()
self.run_timer = False
if run_monitor_object.isAlive():
print 'Thread Alive...'
else:
print 'Thread is Dead....'
class RunMonitor(threading.Thread):
def __init__(self, parent=None):
threading.Thread.__init__(self)
def run(self):
print 'Invoking Command Prompt..........'
subprocess.call(["start", "/DC:\\Scripts", "scripts_to_execute.bat"], shell=True)
When I run this, I get the following error...
UnboundLocalError: local variable 'run_monitor_object' referenced before assignment at if run_monitor_object.isAlive():
Just wondering how else, I could
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码:
错误。如果未采用第一个分支(可能在第二次调用中,当
self.run_timer
已为 True 时),则不会分配run_monitor_object
,并且您尝试在第二个if
。要实现此功能,请将
run_monitor_thread
设置为该类的实例变量。另外,为什么要在定时器中启动线程呢?这只是不必要地使你的逻辑复杂化。创建计时器时启动它。然后将使用计时器来监视它。
This code:
Is wrong. If the first branch isn't taken (perhaps in a second invocation when
self.run_timer
is already True),run_monitor_object
isn't assigned, and you attempt to use it in the secondif
.To make this work, make
run_monitor_thread
an instance variable of the class.Also, why start the thread in the timer? This is just needlessly complicating your logic. Start it when you create the timer. The timer will then be used to monitor it.