python - Using threading in pyQt5, push task to the front of the queue -
i have user interface in big files being imported. can take while, importing of files done in (or several) worker threads. however, files being imported, user should able @ them being imported (i.e. data files plotted ui using matplotlib).
if plotting operation not done in separate thread, not run until importing of files complete. if done in worker thread, operation put onto end of queue.
so, i'm looking way push new operation front of queue, or (even better) run operation in gui thread simultaneous importing files in worker thread.
minimal code example:
class workersignals(qobject): finished = pyqtsignal() error = pyqtsignal(tuple) result = pyqtsignal(object) progress = pyqtsignal(int) class worker(qrunnable): def __init__(self, fn, *args, **kwargs): super(worker, self).__init__() self.fn = fn self.args = args self.kwargs = kwargs self.signals = workersignals() @pyqtslot() def run(self): result = self.fn(*self.args, **self.kwargs) self.signals.result.emit(result) self.signals.finished.emit() class main(qmainwindow, ui_mainwindow): def __init__(self): super(main, self).__init__() self.setupui(self) self.threadpool = qthreadpool() self.displaybutton.clicked.connect(self.displayfile) def loadfiles(self): fpaths, _ = qfiledialog.getopenfilenames(self, '') fpath in fpaths: self.workerstarting("load file") worker = worker(load_files, fpath) worker.signals.result.connect(self.fileloaded) self.threadpool.start(worker) def fileloaded(self, file): """add filename ui""" self.uilistfiles.additem(file.name) def displayfile(): self.figimage.clear() self.figimage.imshow(...) self.figcanvas.draw()
Comments
Post a Comment