python - Any flaw in the logic of using qtimer and qthread? -
i have gui developed using pyqt4 has run button. on run button click, invoke timer , thread. timer keeps monitoring thread. on thread invoke command prompt execute test cases. want thread alive till command prompt opened , want dead once close command prompt.
the code had written achieve below. logic flaws? or better way 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 thread...........' run_monitor_object.start() self.run_timer = false if run_monitor_object.isalive(): print 'thread alive...' else: print 'thread 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 run this, following error...
unboundlocalerror: local variable 'run_monitor_object' referenced before assignment @ if run_monitor_object.isalive():
just wondering how else,
this code:
if self.run_timer: run_monitor_object = runmonitor() print 'starting thread...........' run_monitor_object.start() self.run_timer = false if run_monitor_object.isalive(): print 'thread alive...' else: print 'thread dead....'
is wrong. if first branch isn't taken (perhaps in second invocation when self.run_timer
true), run_monitor_object
isn't assigned, , attempt use in second if
.
to make work, make run_monitor_thread
instance variable of class.
also, why start thread in timer? needlessly complicating logic. start when create timer. timer used monitor it.
Comments
Post a Comment