THreadpools

This commit is contained in:
jens 2022-11-03 18:21:10 +01:00
parent c505a49392
commit c2e2f302a4

View File

@ -48,16 +48,17 @@ class WorkerSignals(QtCore.QObject):
error = QtCore.pyqtSignal(tuple) error = QtCore.pyqtSignal(tuple)
result = QtCore.pyqtSignal(object) result = QtCore.pyqtSignal(object)
progress = QtCore.pyqtSignal(int) progress = QtCore.pyqtSignal(int)
datasignal = QtCore.pyqtSignal(int,float)
# ############################# # #############################
# MULTITHREADING : WORKER CLASS # MULTITHREADING : WORKER CLASS
# ############################# # #############################
class Thread(QtCore.QThread): class Thread(QtCore.QRunnable):
def __init__(self, fn, *args, **kwargs): def __init__(self, fn, *args, **kwargs):
parent = None #parent = None
super(Thread, self).__init__(parent) super().__init__()
self.runs = True self.runs = True
self.fn = fn self.fn = fn
self.args = args self.args = args
@ -153,6 +154,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
#self.microstepping = 1 #self.microstepping = 1
#print(self.microstepping) #print(self.microstepping)
self.threadpool = QtCore.QThreadPool()
def recurring_timer(self): def recurring_timer(self):
self.counter +=1 self.counter +=1
@ -484,8 +487,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending RUN command..") print("Sending RUN command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("RUN command sent.") print("RUN command sent.")
else: else:
self.statusBar().showMessage("No pumps enabled.") self.statusBar().showMessage("No pumps enabled.")
@ -503,8 +506,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending PAUSE command..") print("Sending PAUSE command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("PAUSE command sent.") print("PAUSE command sent.")
self.ui.pause_BTN.setText("Resume") self.ui.pause_BTN.setText("Resume")
@ -517,8 +520,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending RESUME command..") print("Sending RESUME command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("RESUME command sent.") print("RESUME command sent.")
self.ui.pause_BTN.setText("Pause") self.ui.pause_BTN.setText("Pause")
@ -532,8 +535,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending ZERO command..") print("Sending ZERO command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
##jfs ##jfs
self.p1_left=int(self.p1_syringe.split(sep=' ')[1]) self.p1_left=int(self.p1_syringe.split(sep=' ')[1])
self.ui.p1_remain_DISP.display(self.p1_left) self.ui.p1_remain_DISP.display(self.p1_left)
@ -552,8 +555,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending STOP command..") print("Sending STOP command..")
thread = Thread(self.send_single_command, cmd) thread = Thread(self.send_single_command, cmd)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("STOP command sent.") print("STOP command sent.")
def jog(self, btn): def jog(self, btn):
@ -576,8 +579,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending JOG command..") print("Sending JOG command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.fsignal.inished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("JOG command sent.") print("JOG command sent.")
elif btn.text() == "Jog -": elif btn.text() == "Jog -":
@ -587,8 +590,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending JOG command..") print("Sending JOG command..")
thread = Thread(self.runTest, testData) thread = Thread(self.runTest, testData)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.fsignal.inished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("JOG command sent.") print("JOG command sent.")
else: else:
self.statusBar().showMessage("No pumps enabled.") self.statusBar().showMessage("No pumps enabled.")
@ -1034,8 +1037,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending P1 SETTINGS..") print("Sending P1 SETTINGS..")
thread = Thread(self.runTest, self.p1_settings) thread = Thread(self.runTest, self.p1_settings)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.finishesignal.d.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("P1 SETTINGS sent.") print("P1 SETTINGS sent.")
def send_p2_settings(self): def send_p2_settings(self):
@ -1047,8 +1050,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending P2 SETTINGS..") print("Sending P2 SETTINGS..")
thread = Thread(self.runTest, self.p2_settings) thread = Thread(self.runTest, self.p2_settings)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.finishesignal.d.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("P2 SETTINGS sent.") print("P2 SETTINGS sent.")
def send_p3_settings(self): def send_p3_settings(self):
@ -1060,8 +1063,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending P3 SETTINGS..") print("Sending P3 SETTINGS..")
thread = Thread(self.runTest, self.p3_settings) thread = Thread(self.runTest, self.p3_settings)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.finishesignal.d.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
print("P3 SETTINGS sent.") print("P3 SETTINGS sent.")
# Connect to the Arduino board # Connect to the Arduino board
@ -1085,8 +1088,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
# This is a thread that always runs and listens to commands from the Arduino # This is a thread that always runs and listens to commands from the Arduino
#self.global_listener_thread = Thread(self.listening) #self.global_listener_thread = Thread(self.listening)
#self.global_listener_thread.finished.connect(lambda:self.self.thread_finished(self.global_listener_thread)) #self.global_listener_thread.finished.connect(lambda:self.self.thread_finished(self.global_listenersignal._thread))
#self.global_listener_thread.start() #self.global_listener_self.threadpool.start(thread)
# ~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~
# TAB : Setup # TAB : Setup
@ -1141,8 +1144,8 @@ class MainWindow(QtWidgets.QMainWindow, gui2.Ui_MainWindow):
print("Sending all settings..") print("Sending all settings..")
thread = Thread(self.runTest, self.settings) thread = Thread(self.runTest, self.settings)
thread.finished.connect(lambda:self.thread_finished(thread)) thread.signals.finished.connect(lambda:self.thread_finished(thread))
thread.start() self.threadpool.start(thread)
self.ui.p1_setup_send_BTN.setStyleSheet("background-color: none") self.ui.p1_setup_send_BTN.setStyleSheet("background-color: none")
self.ui.p2_setup_send_BTN.setStyleSheet("background-color: none") self.ui.p2_setup_send_BTN.setStyleSheet("background-color: none")