geogeek
geogeek

Reputation: 1312

subprocess.call() do not wait when called from thread

i have a problem with subprocess.call() which do not wait when called from inside a thread.

here is the code that i'm using:

import time
from PyQt4 import QtCore, QtGui
import sys
import subprocess


def tt_fun():
    for a in range(1, 200):
        print  a
##        time.sleep(0.13)
        subprocess.call("timeout 1000")

class SleepProgress(QtCore.QThread):
    procDone = QtCore.pyqtSignal(bool)
    partDone = QtCore.pyqtSignal(int)

    func = None

    def write(self, txt):

        if txt != '':
            try:
                self.partDone.emit(int(txt[:-1]))
            except:pass
    def run(self):
        self.func()
        self.procDone.emit(True)


class AddProgresWin(QtGui.QWidget):
    def __init__(self, func , parent=None ):
        super(AddProgresWin, self).__init__(parent)

        sp =SleepProgress()
        self.thread = sp
        sys.stdout=sp
        self.nameLabel = QtGui.QLabel("0.0%")
        self.nameLine = QtGui.QLineEdit()

        self.progressbar = QtGui.QProgressBar()
        self.progressbar.setMinimum(1)
        self.progressbar.setMaximum(100)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(self.progressbar, 0, 0)
        mainLayout.addWidget(self.nameLabel, 0, 1)

        self.setLayout(mainLayout)
        self.setWindowTitle("Processing")
        self.thread.func =func
        self.thread.partDone.connect(self.updatePBar)
        self.thread.procDone.connect(self.fin)

        self.thread.start()

    def updatePBar(self, val):
        self.progressbar.setValue(val)
        perct = "{0}%".format(val)
        self.nameLabel.setText(perct)

    def fin(self):
        pass


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.path)
    pbarwin = AddProgresWin(tt_fun)
    pbarwin.show()
    sys.exit(app.exec_())

Upvotes: 0

Views: 400

Answers (1)

tdelaney
tdelaney

Reputation: 77407

subprocess.call("timeout 1000") fails with "no such file or directory". You either need to run this through the shell subprocess.call("timeout 1000", shell=True) or pass the program and parameters as a list subprocess.call(["timeout", " 1000"]). The second option will be a bit faster.

Its a good idea to log errors in threads so you know what happens!

def tt_fun():
    try:
        for a in range(1, 200):
            print  a
##            time.sleep(0.13)
            retval = subprocess.check_call("timeout 1000")
    except Exception, e:
        sys.stderr.write("tt_fun error %s\n" % e)
        raise

Upvotes: 2

Related Questions