[add] MultiThread class added

This commit is contained in:
Marcel Paffrath 2017-07-31 10:30:53 +02:00
parent f86f33b22f
commit f13542840f

View File

@ -96,3 +96,68 @@ class Thread(QThread):
def flush(self): def flush(self):
pass pass
class MultiThread(QThread):
finished = Signal(str)
message = Signal(str)
def __init__(self, parent, func, args, ncores=1,
progressText=None, pb_widget=None, redirect_stdout=False):
QThread.__init__(self, parent)
self.func = func
self.args = args
self.progressText = progressText
self.pb_widget = pb_widget
self.redirect_stdout = redirect_stdout
self.finished.connect(self.hideProgressbar)
self.showProgressbar()
def run(self):
if self.redirect_stdout:
sys.stdout = self
try:
pool = multiprocessing.Pool(self.ncores)
self.data = pool.map_async(self.func, self.args, callback=self.emitDone)
#self.data = pool.apply_async(self.func, self.shotlist, callback=self.emitDone) #emit each time returned
pool.close()
self._executed = True
return result
except Exception as e:
self._executed = False
self._executedError = e
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print('Exception: {}, file: {}, line: {}'.format(exc_type, fname, exc_tb.tb_lineno))
sys.stdout = sys.__stdout__
def __del__(self):
self.wait()
def showProgressbar(self):
if self.progressText:
if not self.pb_widget:
self.pb_widget = QDialog(self.parent())
self.pb_widget.setWindowFlags(Qt.SplashScreen)
self.pb_widget.setModal(True)
hl = QHBoxLayout()
pb = QProgressBar()
pb.setRange(0, 0)
hl.addWidget(pb)
hl.addWidget(QLabel(self.progressText))
self.pb_widget.setLayout(hl)
self.pb_widget.show()
def hideProgressbar(self):
if self.pb_widget:
self.pb_widget.hide()
def write(self, text):
self.message.emit(text)
def flush(self):
pass
def emitDone(self, result):
print('emitDone!')
self.finished.emit('Done thread!')
self.hideProgressBar()