简体   繁体   中英

Threading in Python not separating UI from worker thread

I am missing here something. Albeit running in two seperate threads, the UI is still not updated as expected. It is still lagging on the dd worker thread.

from ui import Ui_main_window
from PyQt4 import QtGui, QtCore

import sys
import subprocess
import commands
import threading

from time import sleep

out_int = 0

def _dd_thread_run(_if, _of, _bs, _size):
    _dd_subprocess_command_format = "dd if=%s bs=%s | pv -n --size %s | dd of=%s" % (_if, _bs, _size, _of)
    _dd_subprocess_command = [_dd_subprocess_command_format]
    _dd_progress = subprocess.Popen(_dd_subprocess_command, shell=True, stderr=subprocess.PIPE)
    while _dd_progress.poll() is None:
        out = _dd_progress.stderr.readline().replace("\n", "")
        global out_int 
        out_int = int (out)

def _ui_progress_set():
    class MainWindow(QtGui.QMainWindow, Ui_main_window):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)

    app = QtGui.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    while True:
        for i in range(100):
            ui.progressBar.setValue(out_int)
            sleep(.1)


t1 = threading.Thread(target=_dd_thread_run, args = ["/dev/urandom", "/dev/null", "100K", "100M"])
t1.start()
t2 = threading.Thread(target=_ui_progress_set, args = [])
t2.start()

I suspect a Python or PyQt bug? And it stays the same, no matter where the UI class is defined.

You don't have the main eventloop running anywhere - you won't get a gui to show up without that ( see doc ). Try

app = QtGui.QApplication(sys.argv)
ui = MainWindow()
ui.show()
app.exec_()

without the while loop. If your while loop is supposed to create an indefinite progressbar - that can easily be achieved by setting the QProgressBar minimum and maximum to zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM