Results 1 to 20 of 20

Thread: QThread with Python Popen freezing on sizable external program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default QThread with Python Popen freezing on sizable external program

    As it stands, a QThread worker class enables the external call of an *.exe with Popen. While this external process is running, a QProgressBar within the governing class needs to be periodically updated.

    The sample below works for a few percentage increments, then it suddenly freezes until the process is complete.

    Qt Code:
    1. class workerThread(QThread):
    2. progress = pyqtSignal(int)
    3.  
    4. def runProgram(self,directory):
    5. file = directory+'fileName.txt'
    6. program = sys.path[4] + "/Root/GIS_Pre_Processor.exe"
    7. runProgram = Popen([program,file],shell=True,cwd=directory)
    8. currentProgress = 45
    9. while runProgram.poll() is None:
    10. time.sleep(1.5) #<=== makes it through a couple of these loops
    11. currentProgress = currentProgress + 5
    12. self.progress.emit(currentProgress)
    13. self.progress.emit(100)
    To copy to clipboard, switch view to plain text mode 

    Does this suggest an improper implementation of QThread?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread with Python Popen freezing on sizable external program

    What is the reason for the while loop? Why don't you use a timer or QTimeLine? And QProcess for handling the worker process.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    jkrienert (7th March 2015)

  4. #3
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: QThread with Python Popen freezing on sizable external program

    I had not yet investigated QProcess, but currently I am having trouble implementing it to any functioning result.

    Qt Code:
    1. fileName = someDirectory+'anInterestingFile.txt'
    2. Program = sys.path[4] + "/someDirectory/console.exe"
    3. runTheProgram = QProcess()
    4. runTheProgram.setWorkingDirectory(outsideDirectory)
    5. runTheProgram.finished.connect(self.parseResults)
    6. runTheProgram.execute(Program,fileName)
    To copy to clipboard, switch view to plain text mode 

    Where 'fileName' is the first and only argument to be entered into the console 'Program'.
    The program itself closes automatically wether successful or not, but I would like to somehow check for assurance that it indeed has
    ran and executed the singular argument 'fileName' sent.

    Currently it closes without executing the 'fileName' argument.
    Last edited by jkrienert; 7th March 2015 at 22:22.

  5. #4
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: QThread with Python Popen freezing on sizable external program

    The implementation of QProcess must be off-tilt, as with the following example, the Ui hangs just as with the initial postings use of POpen...
    Granted, the program does run - but eventually crashes in the process (after ~20 sec.) without any progressBar updates sent where as with the identical 'NAMfile' command used with POpen
    results in success.



    Qt Code:
    1. NAMfile = str(self.modDirectory)+'mfnam.txt'
    2. MODFLOW = sys.path[4] + "/GraphicGroundwater/mf2005.exe"
    3.  
    4. qMODFLOW = QProcess()
    5. qMODFLOW.setWorkingDirectory(self.modDirectory)
    6. qMODFLOW.finished.connect(self.returnResultStats)
    7. qMODFLOW.start(MODFLOW,[r"%s"%(NAMfile)])
    8.  
    9. qMODFLOW.waitForFinished()
    10. while not qMODFLOW.ExitStatus():
    11. time.sleep(0.5)
    12. currentProgress = currentProgress + 2
    13. self.progress.emit(currentProgress)
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QThread with Python Popen freezing on sizable external program

    I'm not entirely sure I understand what you're doing, but a couple of observations:

    - QProcess:waitForFinished() will block for 30,000 milliseconds (30 seconds). If your process ends before 30 seconds is up, it will return. It will also return after 30 seconds expires even if your QProcess is still running. Just used the QProcess signals to get state changes and/or finished notification, etc.

    - Not sure what your while loop is trying to accomplish. You will get the process exit code and exit status in the finished signal. Use that to determine whether your process ran successfully.

    - Your progress bar updates are likely not reflecting the completion status since you are arbitrarily updating your progress every .5 seconds. How many seconds will the process run to completion, does it vary?

    - You can read output of your QProcess, so if it's outputing any information while running, you can consume that output to get a better completion status if your process normally displays this. I have used QProcess like this in the past where i read the stdout and stderr of the QProcess to determine progress, etc.

    Good luck.
    Last edited by jefftee; 8th March 2015 at 02:45.

  7. The following user says thank you to jefftee for this useful post:

    jkrienert (8th March 2015)

Similar Threads

  1. PyQt4: Create simple GUI for python program
    By Norchina in forum Newbie
    Replies: 0
    Last Post: 11th June 2010, 14:50
  2. Replies: 1
    Last Post: 30th April 2010, 13:25
  3. running external console program by gui program
    By alireza.mixedreality in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 18:05
  4. Replies: 1
    Last Post: 15th February 2010, 23:20
  5. Replies: 7
    Last Post: 19th January 2008, 15:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.