Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active July 11, 2019 11:02
Show Gist options
  • Select an option

  • Save justinfx/5174795 to your computer and use it in GitHub Desktop.

Select an option

Save justinfx/5174795 to your computer and use it in GitHub Desktop.

Revisions

  1. justinfx revised this gist Mar 16, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions testProgram.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/bin/bash

    for i in {1..5}
    do echo "Progress: $i"
    sleep 1
    done
  2. justinfx created this gist Mar 16, 2013.
    66 changes: 66 additions & 0 deletions read_qt_process.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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_()