i trying figure out way qprocess (linux!) works because i'm going need project of mine (note: suprocess or multithreading not used! process has detached main application!). here small code demonstrate basic functionality:
#!/usr/bin/python import sys pyqt4 import qtgui, qtcore pyqt4.qtcore import qprocess class example(qtgui.qwidget): def __init__(self): super(example, self).__init__() self.myprocess = qprocess(self) self.myprocess.finished.connect(self.onfinished) # never called self.myprocess.statechanged.connect(self.onstatechanged) # never called self.myprocess.started.connect(self.onstarted) # never called self.command = "./testcommand.py" self.args = [""] self.initui() def initui(self): hbox = qtgui.qhboxlayout() hbox.addstretch(1) qbtn = qtgui.qpushbutton('start', self) qbtn.clicked.connect(self.toggleprocess) qbtn.resize(qbtn.sizehint()) hbox.addwidget(qbtn) # button testing responsiveness of gui after qprocess has been started qbtn2 = qtgui.qpushbutton('click me', self) qbtn2.setcheckable(true) qbtn2.toggled.connect(self.togglebutton) qbtn2.resize(qbtn2.sizehint()) hbox.addwidget(qbtn2) self.setlayout(hbox) self.setgeometry(300, 300, 250, 150) self.setwindowtitle('qprocess controlled button') self.show() def toggleprocess(self): # process states (based on qt docs): # 0 - not running # 1 - starting # 2 - running # reason state 0 if self.myprocess.state() == 0: self.myprocess.startdetached(self.command, self.args) print "starting process" print "process state", str(self.myprocess.state()) elif self.myprocess.state() == 1: print "process starting" return else: print "stopping process" self.myprocess.close() def togglebutton(self, value): if value == true: print "lalalala!" else: print "didadida!" def onstarted(self): print "process started" def onfinished(self): print "process stopped" def onstatechanged(self): print "process has changed state" def __del__(self): if self.myprocess.state() == 1 or self.myprocess.state() == 2: self.myprocess.close() else: pass def main(): app = qtgui.qapplication(sys.argv) ex = example() sys.exit(app.exec_()) if __name__ == '__main__': main() the testcommand.py follows:
version 1:
#!/usr/bin/env python count = 0 while count < 10: print "c" count = count + 1 version 2: here try infinite process see if gui blocked or not.
#!/usr/bin/env python while true: print "c" here i've encountered multiple issues. first of qprocess.state() function returns 0 hence never land in 2 other cases of if statement inside toggleprocess() function. because of absence of change in process' state none of signals gets emitted...ever. no matter if pick version 1 of testcommand.py, runs loop 10 times or version 2, runs infinitely till process closed, result state 0 though can see process running (in version 2 infinite lines of "c" characters). in case use version 2 accordingly unable stop process (because state not change hence qprocess.close() never called) if close main application create orphaned process has killed via htop or similar process manager. know qprocess.startdetached() creates detached process still hope have control on execution of process via "start" button. btw same behaviour when use qprocess.execute(), creates sub-process , accordingly freezes gui time requires run (in version 2 indefinitely).
can tell me why happening? want able start detached process still able terminate via same button. have tried checkable button (like "click me" button) , boolean flag, issue missing change of state still there.
thanks!
the startdetached function static. detached process started internally qt, , there never qprocess corresponds it. why signals not work. in example script, myprocess redundant.
by definition, detached process has no direct means of communication process started it. pid, , that's it. on platforms may possible use pid kill process - see os.kill, instance.
for same reasons above, there no way re-attach existing process. have pid, need stored externally somehow (e.g. in file) if want re-use later.
broadly speaking, problem dealing inter-process communication (ipc). there many different solutions available, need clearer idea of structure of application before deciding 1 appropriate.
Comments
Post a Comment