Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active July 11, 2019 11:02
Show Gist options
  • Save justinfx/5174795 to your computer and use it in GitHub Desktop.
Save justinfx/5174795 to your computer and use it in GitHub Desktop.
2 different examples of reading the progressive output of a process in PyQt. 1) Using a QProcess 2) Using subprocess [https://groups.google.com/d/msg/python_inside_maya/x9COZGNPGEU/gD4xpbs3S08J]
from functools import partial
from subprocess import Popen, PIPE
from PyQt4 import QtCore, QtGui
## testProgram.sh
"""
#!/bin/bash
for i in {1..5}
do echo "Progress: $i"
sleep 1
done
"""
COMMAND = "/path/to/testProgram.sh "
def started():
print "Started!"
def finished():
QtGui.qApp.quit()
### 1) QProcess ###
def dataReady(p):
print str(p.readAllStandardOutput()).strip()
def main():
process = QtCore.QProcess()
process.started.connect(started)
process.readyReadStandardOutput.connect(partial(dataReady, process))
process.finished.connect(finished)
# start it some time after the event loop
QtCore.QTimer.singleShot(100, partial(process.start, COMMAND))
### 2) Subprocess ###
def process():
started()
process = Popen([COMMAND], stdout=PIPE)
while True:
line = process.stdout.readline()
if not line:
break
QtGui.qApp.processEvents()
print line.strip()
finished()
def main2():
# start it some time after the event loop
QtCore.QTimer.singleShot(100, process)
if __name__ == "__main__":
app = QtGui.QApplication([])
main()
# main2()
app.exec_()
#!/bin/bash
for i in {1..5}
do echo "Progress: $i"
sleep 1
done
@tweak-wtf
Copy link

tweak-wtf commented Jul 11, 2019

oh this is exactly what I've been looking for!
Thanks for putting this up :)
was trying to implement this utilising QThreads... am quite a novice in Qt 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment