Results 1 to 1 of 1

Thread: QThread to run one or more QProcess

  1. #1
    Join Date
    Jul 2010
    Location
    United States
    Posts
    13
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: QThread to run one or more QProcess

    I am having some problems with running one or more QProcess's in a single QThread. My goal is to run one process at a time in a single thread, one after another. I am able to successfully do that but as these process's run I am noticing that my memory for the python GUI just starts consuming all of the memory over time. I made basic little command line programs which simply iterate to a value to simulate a long running program.

    When I start a process I wait for it to end using waitForFinished(-1). I assume since this is being done in a thread it should not affect my core Python GUI. Am I not using the QProcess within a QThread correctly? If I take out the waitForFinished(-1) then I always get an error that the thread was destroyed before it had completed. I am attaching the Python code below

    Thanks In Advanced!!

    Qt Code:
    1. #!/usr/bin/env python
    2. '''
    3. Try making a responsive gui with programs running in the background
    4. '''
    5.  
    6. # Python modules
    7. import sys
    8.  
    9. # PyQt4 Modules
    10. from PyQt4.QtCore import *
    11. from PyQt4.QtGui import *
    12.  
    13. class ThreadTest(QDialog):
    14.  
    15. def __init__(self, parent=None):
    16. super(ThreadTest, self).__init__(parent)
    17.  
    18. self.setMinimumWidth(400)
    19. self.setMinimumHeight(400)
    20. self.setWindowTitle("Thread Test")
    21.  
    22. self.thread = IndividualThread()
    23.  
    24. self.pbar = QProgressBar()
    25. self.label = QLabel("")
    26. self.p1Btn = QPushButton("Run Program 1")
    27. self.p2Btn = QPushButton("Run Program 2")
    28. self.p3Btn = QPushButton("Run Program 3")
    29.  
    30. self.pbar.setOrientation(Qt.Horizontal)
    31. self.pbar.setRange(0,100)
    32. self.pbar.setValue(0)
    33. self.pbar.setInvertedAppearance(False)
    34. self.pbar.setTextVisible(False)
    35.  
    36. box = QVBoxLayout()
    37. box.addWidget(self.p1Btn)
    38. box.addWidget(self.p2Btn)
    39. box.addWidget(self.p3Btn)
    40. box.addWidget(self.label, 0, Qt.AlignCenter)
    41. box.addWidget(self.pbar)
    42.  
    43. self.setLayout(box)
    44.  
    45. self.timer = QTimer()
    46.  
    47. self.connect(self.p1Btn , SIGNAL('pressed()'), self.runP1)
    48. self.connect(self.p2Btn , SIGNAL('pressed()'), self.runP2)
    49. self.connect(self.p3Btn , SIGNAL('pressed()'), self.runP3)
    50.  
    51. self.connect(self.timer , SIGNAL('timeout()') , self.onTimeout)
    52. self.connect(self.thread , SIGNAL('started()') , self.onThreadStarted)
    53. self.connect(self.thread , SIGNAL('finished()'), self.onThreadFinished)
    54.  
    55. self.connect(self.dsn_thread, SIGNAL('threadInfo'), self.onThreadInfo)
    56.  
    57. def enableButtons(self, enable):
    58. self.p1Btn.setEnabled(enable)
    59. self.p2Btn.setEnabled(enable)
    60. self.p3Btn.setEnabled(enable)
    61. self.dsnBtn.setEnabled(enable)
    62.  
    63. def runP1(self):
    64.  
    65. self.label.setText("Running program 1")
    66. self.thread.setCommand("/home/hall000s/python/threads/prog1_lnx")
    67. self.thread.start()
    68.  
    69. def runP2(self):
    70. self.label.setText("Running program 2")
    71. self.thread.setCommand("/home/hall000s/python/threads/prog2_lnx")
    72. self.thread.start()
    73.  
    74. def runP3(self):
    75. self.label.setText("Running program 3")
    76. self.thread.setCommand("/home/hall000s/python/threads/prog3_lnx")
    77. self.thread.start()
    78.  
    79. def onThreadInfo(self, msg):
    80. self.label.setText(msg)
    81.  
    82. def onTimeout(self):
    83. # Update the progress bar
    84. value = self.pbar.value()
    85.  
    86. # Going forward or backwards?
    87. if self.pbar.invertedAppearance():
    88. if value-2 < self.pbar.minimum():
    89. self.pbar.setValue(self.pbar.minimum())
    90. self.pbar.setInvertedAppearance(False)
    91. else:
    92. self.pbar.setValue(value-2)
    93. else:
    94. if value+2 > self.pbar.maximum():
    95. self.pbar.setValue(self.pbar.maximum())
    96. self.pbar.setInvertedAppearance(True)
    97. else:
    98. self.pbar.setValue(value+2)
    99.  
    100. def onThreadStarted(self):
    101. print "Thread has been started"
    102. self.timer.start(10)
    103. self.enableButtons(False)
    104.  
    105. def onThreadFinished(self):
    106. print "Thread has finished"
    107. self.timer.stop()
    108. self.enableButtons(True)
    109. self.pbar.setValue(0)
    110. self.label.setText("")
    111.  
    112. class IndividualThread(QThread):
    113.  
    114. def __init__(self, parent=None):
    115. super(IndividualThread, self).__init__(parent)
    116.  
    117. self.args = ""
    118. self.command = ""
    119.  
    120. def setArguments(self, arguments):
    121. self.args = arguments
    122.  
    123. def setCommand(self, command):
    124. self.command = command
    125.  
    126. def run(self):
    127. process = QProcess()
    128. process.start(self.command, self.args)
    129. process.waitForFinished(-1)
    130.  
    131. #
    132. # MAIN
    133. #
    134. if __name__ == '__main__':
    135.  
    136. # Order matters from here on.
    137. app = QApplication(sys.argv)
    138. form = ThreadTest()
    139. form.show()
    140. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 


    Added after 1 33 minutes:


    Ignore this, I figured out the problem. I wasn't paying attention to the amount of data I was writing out to stderr and stdout in my silly program, which was causing the memory to increase as the program executed.
    Last edited by smhall316; 27th September 2011 at 20:39.

Similar Threads

  1. [Article] How to QProcess in QThread
    By Raccoon29 in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2009, 08:22
  2. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 19:15
  3. QThread and QProcess problem
    By codebehind in forum Qt Programming
    Replies: 13
    Last Post: 7th August 2007, 08:11
  4. Replies: 4
    Last Post: 27th July 2006, 11:13
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.