qtimer和qthread的使用逻辑有什么缺陷吗?

发布于 2024-11-06 05:47:47 字数 1157 浏览 1 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云归处 2024-11-13 05:47:47

此代码:

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....'

错误。如果未采用第一个分支(可能在第二次调用中,当 self.run_timer 已为 True 时),则不会分配 run_monitor_object,并且您尝试在第二个if

要实现此功能,请将 run_monitor_thread 设置为该类的实例变量。

另外,为什么要在定时器中启动线程呢?这只是不必要地使你的逻辑复杂化。创建计时器时启动它。然后将使用计时器来监视它。

This code:

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....'

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 second if.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文