I've inhereted a GUI code which is structured something like this:
any button signal triggers a slot, those slots then call an external process to receive information and wait until that process finishes, then the slot proceeds. the issue is, this external process takes between 0.5 to 60 seconds, and in that time the GUI freezes. i'm struggling to find a good way to seperate this process call to a different thread or QProcess (that way i will not block the main event loop) and then return and continue the relevent slot (or function) from that same point with the information received from that external slow process. generators seem like something that should go here, but im struggling to figure how to restructure the code so this will work.
any suggestions or ideas? is there a Qt way to "yield" a function until that process completes and then continue that function?
Psudo code of the current structure:
button1.clicked.connect(slot1)
button2.clicked.connect(slot2)
def slot1():
status = subprocess.run("external proc") # this is blocking
...
...
return
def slot2():
status = subprocess.run("external proc") # this is blocking
...
...
return
Here is the code with the example I was mentioning in the comments:
class MainWindow(QMainWindow, ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
ui_MainWindow.__init__(self)
self.setupUi(self)
self.button_1.clicked.connect(lambda: self.threaded_wait(1))
self.button_5.clicked.connect(lambda: self.threaded_wait(5))
self.button_10.clicked.connect(lambda: self.threaded_wait(10))
#Start a timer that executes every 0.5 seconds
self.timer = QtCore.QBasicTimer()
self.timer.start(500, self)
#INIT variables
self.results = {}
self.done = False
def timerEvent(self, event):
#Executes every 500msec.
if self.done:
print(self.results)
self.done = False
def threaded_wait(self, time_to_wait):
self.done = False
new_thread = threading.Thread(target=self.actual_wait, args=(time_to_wait,self.sender().objectName()))
new_thread.start()
def actual_wait(self, time_to_wait: int, button_name):
print(f"Button {button_name} Pressed:\nSleeping for {int(time_to_wait)} seconds")
time_passed = 0
for i in range(0, time_to_wait):
print(int( time_to_wait - time_passed))
time.sleep(1)
time_passed = time_passed + 1
self.results[button_name] = [1,2,3,4,5]
self.done = True
print("Done!")
You can use QThread. With Qthread you can pass arguments to a function in mainWindow with signal mechanism.
Here is a source that explains how to use Qthread:
https://realpython.com/python-pyqt-qthread/
if you read the soruce it will be helpfull to you, i think. And there is a sample gui in the page, i write it down to you(you can run it):
from PyQt5.QtCore import QObject, QThread, pyqtSignal
from PyQt5.QtWidgets import QMainWindow
import time
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
import sys
# Snip...
# Step 1: Create a worker class
#
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)
def run(self):
"""Long-running task."""
for i in range(5):
time.sleep(1)
self.progress.emit(i + 1)
self.finished.emit()
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.clicksCount = 0
self.setupUi()
def setupUi(self):
self.setWindowTitle("Freezing GUI")
self.resize(300, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
# Create and connect widgets
self.clicksLabel = QLabel("Counting: 0 clicks", self)
self.clicksLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.stepLabel = QLabel("Long-Running Step: 0")
self.stepLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.countBtn = QPushButton("Click me!", self)
self.countBtn.clicked.connect(self.countClicks)
self.longRunningBtn = QPushButton("Long-Running Task!", self)
self.longRunningBtn.clicked.connect(self.runLongTask)
# Set the layout
layout = QVBoxLayout()
layout.addWidget(self.clicksLabel)
layout.addWidget(self.countBtn)
layout.addStretch()
layout.addWidget(self.stepLabel)
layout.addWidget(self.longRunningBtn)
self.centralWidget.setLayout(layout)
def countClicks(self):
self.clicksCount += 1
self.clicksLabel.setText(f"Counting: {self.clicksCount} clicks")
def reportProgress(self, n):
self.stepLabel.setText(f"Long-Running Step: {n}")
def runLongTask(self):
# Step 2: Create a QThread object
self.thread = QThread()
# Step 3: Create a worker object
self.worker = Worker()
# Step 4: Move worker to the thread
self.worker.moveToThread(self.thread)
# Step 5: Connect signals and slots
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.reportProgress)
# Step 6: Start the thread
self.thread.start()
# Final resets
self.longRunningBtn.setEnabled(False)
self.thread.finished.connect(
lambda: self.longRunningBtn.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.stepLabel.setText("Long-Running Step: 0")
)
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
Usually what I do is have the button press run a function that launches a thread to do the work for me.
In my example I have 3 buttons. One that waits for one second, another that waits for 5, and another that waits for 10.
I connect the button slots when they are clicked to threaded_wait() and I use lambda because I want to pass that method an integer argument on how long to wait for (Waiting in this example is just fake processing time).
Then I have the method actual_wait() which is the code that is actually waiting, which is being executed by the thread. Since there is a thread running that code, the main GUI event loop exits the threaded_wait() method right after starting the thread and it is allowed to continue it's event loop
class MainWindow(QMainWindow, ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
ui_MainWindow.__init__(self)
self.setupUi(self)
self.button_1.clicked.connect(lambda: self.threaded_wait(1))
self.button_5.clicked.connect(lambda: self.threaded_wait(5))
self.button_10.clicked.connect(lambda: self.threaded_wait(10))
def threaded_wait(self, time_to_wait):
new_thread = threading.Thread(target=self.actual_wait, args=(time_to_wait,))
new_thread.start()
def actual_wait(self, time_to_wait: int):
print(f"Sleeping for {int(time_to_wait)} seconds")
time_passed = 0
for i in range(0, time_to_wait):
print(int( time_to_wait - time_passed))
time.sleep(1)
time_passed = time_passed + 1
print("Done!")
This prevents my GUI from freezing up.
EDIT:
Sorry as for the second part of your question, if you want to wait for the thread to finish before doing something else, you can use a flag like this:
def actual_wait(self, time_to_wait: int):
print(f"Sleeping for {int(time_to_wait)} seconds")
....
self.DONE = True
And check that self.DONE flag wherever you need it.
It kind of depends what you mean by wait for it to complete.
I think if you use QThread you can also emit a signal when the thread is done and connect that signal to whatever slot after that, but I haven't used QThread.
Related
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi('wind.ui', self)
self.startButton.clicked.connect(self.start_screen)
self.stopButton.clicked.connect(self.stop_screen)
self.screen_thread = Screenshot()
def start_screen(self):
self.screen_thread.start()
self.screen_thread.any_signal.connect(self.myfunction)
def stop_screen(self):
self.screen_thread.stop()
def myfunction(self,value):
vl = value
print(vl, threading.active_count())
class Screenshot(QThread):
any_signal = pyqtSignal(int)
def __init__(self, parent = None):
super(Screenshot, self).__init__(parent)
def run(self):
value = 1
print('process start')
print(threading.active_count())
print(threading.enumerate())
while(True):
value += 1
time.sleep(1)
self.any_signal.emit(value)
def stop(self):
print('process stop')
self.terminate()
print(self.isRunning())
def application():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
application()
There are two start and stop buttons. when I run it for the first time, everything is ok, the value is printed once and incremented. then I press stop, everything stops. when I press start a second time, the result is printed twice. the third time three times, and so on. I thought that new threads were being added and entered a command to monitor threads. but only one thread is shown each time. where is the mistake?
I thought that new threads were being added and entered a command to monitor threads. but only one thread is shown each time.
Only one thread is shown each time because you only create one thread: self.screen_thread = Screenshot()
Each time you click the start button it starts the thread (calling .start() once a thread has started does nothing), and it connects the self.myfunction slot to the any_signal. Because you can connect the same slot multiple times to the same signal, it just gets called multiple times.
If you want multiple threads, then you will need to create them, and keep track of them. Something like this should do:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi('wind.ui', self)
self.startButton.clicked.connect(self.start_screen)
self.stopButton.clicked.connect(self.stop_screen)
self.threads = []
def start_screen(self):
thread = Screenshot()
thread.any_signal.connect(self.myfunction)
thread.start()
self.threads.append(thread)
def stop_screen(self):
for thread in self.threads:
thread.stop()
self.threads = []
def myfunction(self,value):
vl = value
print(vl, threading.active_count())
class Screenshot(QThread):
any_signal = pyqtSignal(int)
def __init__(self, parent = None):
super(Screenshot, self).__init__(parent)
def run(self):
value = 1
print('process start')
print(threading.active_count())
print(threading.enumerate())
while(True):
value += 1
time.sleep(1)
self.any_signal.emit(value)
def stop(self):
print('process stop')
self.terminate()
print(self.isRunning())
def application():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
application()
Note that the use of QThread.terminate is discouraged:
Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.
Instead, call QThread.requestInterruption and regularly check for an interruption request within the thread with QThread.isInterruptionRequested.
I want to use PySide2 Qtcore.Qthread because of Qtcore.Signal, but I end up with this error:
Process finished with exit code -1073740791
from PySide2.QtCore import QThread
class Thread(QThread):
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
Thread().start()
expect to have those prints but I have this error:
Process finished with exit code -1073740791
Update:
so, why this code also throw the same error?
class Thread(QThread):
done = Signal()
def __init__(self):
super(Thread, self).__init__()
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
self.done.emit()
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
btn.show()
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
def done(self):
print('done')
app = QtWidgets.QApplication()
window = Widget()
window.show()
sys.exit(app.exec_())
Explanation
If you run your code in a CMD/Terminal you will get the following error:
QThread: Destroyed while thread is still running
Aborted (core dumped)
And the error is caused because the thread is destroyed while it is still running since it is a local variable, on the other hand QThread needs an event loop to run
Solution
import sys
from PySide2.QtCore import QCoreApplication, QThread
class Thread(QThread):
def run(self):
print("task started")
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print("task finished")
if __name__ == "__main__":
# create event loop
app = QCoreApplication(sys.argv)
th = Thread()
th.start()
th.finished.connect(QCoreApplication.quit)
sys.exit(app.exec_())
Update:
"t" is a local variable that will be eliminated after executing clicked causing the same problem as your initial code, the solution is to prevent it from being destroyed instantly and for this there are 2 options:
Make a "t" class attribute
def clicked(self):
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()
Store the QThread in a container that has a longer life cycle:
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
self.container = []
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
self.container.append(t)
# ...
Pass it as a parent to "self" but for this it is necessary that Thread allow to receive so you must implement that in the constructor:
class Thread(QThread):
done = Signal()
def __init__(self, parent=None):
super(Thread, self).__init__(parent)
# ...
def clicked(self):
t = Thread(self)
t.done.connect(self.done)
t.start()
I found the solution but I don't know why I should do this. the thread should a part of the class.
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()
Is there a way to pass multiple parameters from a thread back to the main thread at the same time?
I have started a thread using PyQt5 in which two real time variables are calculated. I want to pass both parameters back to the main thread to be combined and calculated with parameters from another thread.
As attached in the code below, I am able to return each parameter individually and print to the screen. How do I return all parameters into one function so I can proceed with calculations from another thread?
Thank you!
import sys
import time
from PyQt5.QtWidgets import QMainWindow, QPushButton, QVBoxLayout, QFrame, QApplication
from PyQt5.QtCore import pyqtSignal, QObject, QThread
class Counter(QObject):
'''
Class intended to be used in a separate thread to generate numbers and send
them to another thread.
'''
param1 = pyqtSignal(str)
param2 = pyqtSignal(str)
stopped = pyqtSignal()
def __init__(self):
QObject.__init__(self)
def start(self):
'''
Count from 0 to 99 and emit each value to the GUI thread to display.
'''
for x in range(4):
self.param1.emit(str(x))
self.param2.emit(str(x)+'2')
time.sleep(0.7)
self.stopped.emit()
class Application(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# Configuring widgets
self.button = QPushButton()
self.button.setText('99')
self.layout = QVBoxLayout()
self.layout.addWidget(self.button)
self.frame = QFrame()
self.frame.setLayout(self.layout)
self.setCentralWidget(self.frame)
# Configuring separate thread
self.counterThread = QThread()
self.counter = Counter()
self.counter.moveToThread(self.counterThread)
# Connecting signals
self.button.clicked.connect(self.startCounting)
self.counter.param1.connect(self.button.setText)
self.counter.param1.connect(self.someFunction1)
self.counter.param2.connect(self.someFunction2)
self.counter.stopped.connect(self.counterThread.quit)
self.counterThread.started.connect(self.counter.start)
# print data from parameter 1
def someFunction1(self, data):
print(data + ' in main')
# print data from parameter 2
def someFunction2(self, data):
print(data + ' in main')
def startCounting(self):
if not self.counterThread.isRunning():
self.counterThread.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Application()
window.show()
sys.exit(app.exec_())
The signals also support the transmission of lists so you can use it to transport several variables:
class Counter(QObject):
"""
Class intended to be used in a separate thread to generate numbers and send
them to another thread.
"""
params = pyqtSignal(list)
stopped = pyqtSignal()
def start(self):
"""
Count from 0 to 99 and emit each value to the GUI thread to display.
"""
for x in range(4):
values = [str(x), str(x) + "2"]
self.params.emit(values)
time.sleep(0.7)
self.stopped.emit()
class Application(QMainWindow):
def __init__(self):
super(Application, self).__init__()
# Configuring widgets
self.frame = QFrame()
self.button = QPushButton("99")
lay = QVBoxLayout(self.frame)
lay.addWidget(self.button)
self.setCentralWidget(self.frame)
# Configuring separate thread
self.counterThread = QThread()
self.counter = Counter()
self.counter.moveToThread(self.counterThread)
# Connecting signals
self.button.clicked.connect(self.startCounting)
self.counter.params.connect(self.someFunction)
self.counter.stopped.connect(self.counterThread.quit)
self.counterThread.started.connect(self.counter.start)
#pyqtSlot(list)
def someFunction(self, params):
print(params)
if params:
self.button.setText(params[0])
def startCounting(self):
if not self.counterThread.isRunning():
self.counterThread.start()
I have an application in which I would like to do the following thing:
optimize a problem
wait for a certain amount of time, e.g. one minute
measure a certain property
repeat steps two and three several times
start again at 1.)
I want to start the entire process when clicking on a QPushButton. It is necessary that the step 2.) only starts when step 1.) is completely terminated. I dont know how long the optimzation process takes, therefre I cant just use QTimer.sleep().
I have solved this problem the following way:
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5 import QtWidgets
import sys
class MyForm():
def __init__(self):
self.ui = QDialog()
self.button = QtWidgets.QPushButton(self.ui)
self.button.clicked.connect(self.start_timer)
self.waiting_interval = 10000
self.ui.show()
def start_timer(self):
self.optimize()
self.counter = 0
self.timer = QTimer()
self.timer.timeout.connect(self.tick)
self.timer.setSingleShot(True)
self.timer.start(self.waiting_interval)
def tick(self):
self.timer = QTimer()
if self.counter == 9:
self.timer.timeout.connect(self.start_timer)
else:
self.measure_property()
self.timer.timeout.connect(self.tick)
self.timer.setSingleShot(True)
self.timer.start(self.waiting_interval)
self.counter += 1
def optimize(self):
pass
def measure_property(self):
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
w=MyForm()
app.exec_()
It produces the results that I want but I am looking for a smarter way to do this, maybe using signals and slots. Any help would be appreciated!
The tasks that take a long time are heavy and tend to freeze the GUI giving a bad experience to the user, in these cases those tasks must be executed in another thread:
import sys
from PyQt5 import QtCore, QtWidgets
class ProcessThread(QtCore.QThread):
def run(self):
while True:
self.optimize()
for _ in range(3):
QtCore.QThread.sleep(60)
self.measure_property()
def optimize(self):
print("optimize")
def measure_property(self):
print("measure_property")
class MyForm():
def __init__(self):
self.ui = QtWidgets.QDialog()
self.thread = ProcessThread(self.ui)
self.button = QtWidgets.QPushButton("Press me")
self.button.clicked.connect(self.thread.start)
self.waiting_interval = 10000
lay = QtWidgets.QVBoxLayout(self.ui)
lay.addWidget(self.button)
self.ui.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w=MyForm()
sys.exit(app.exec_())
I wish to open a PySide/PyQt window and automatically start executing a method, which will show a progress in the UI while it is executing.
With the code below, the ui is not shown until the process has completed, thus you cannot see the progress. How can I change the code to see the progress before the process has completed?
from PySide import QtGui
import time
class MyApp(QtGui.QMainWindow)
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
# Setup
self.centralWidget = QtGui.QWidget(self)
self.setCentralWidget(self.centralWidget)
self.setup_UI()
# Execute process!
self.process()
def setup_UI(self):
''' Attach widgets to window '''
self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
self.list_widget = QtGui.QListWidget()
self.progress_bar = QtGui.QProgressBar()
self.mainLayout.addWidget(self.list_widget)
self.mainLayout.addWidget(self.progress_bar)
def process(self):
''' Manipulate the ui '''
self.progress_bar.setMaximum(0)
self.progress_bar.setMaximum(10)
for x in range(0, 10):
time.sleep(1)
self.list_widget.addItem('Item ' + str(x))
self.progress_bar.setValue(x)
my_app = MyApp()
my_app.show()
Your main problem that you are blocking qt main thread by calling time.sleep. To solve this issue you have two options. One of them is using threading. Another option is change your code to asynchronous like this:
from PySide import QtGui, QtCore
import time
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
# Setup
self.centralWidget = QtGui.QWidget(self)
self.setCentralWidget(self.centralWidget)
self.setup_UI()
# Execute process!
self.set_process()
self.timer = QtCore.QTimer()
self.i = 0
self.timer.timeout.connect(self.update)
self.timer.start(1000)
def setup_UI(self):
''' Attach widgets to window '''
self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
self.list_widget = QtGui.QListWidget()
self.progress_bar = QtGui.QProgressBar()
self.mainLayout.addWidget(self.list_widget)
self.mainLayout.addWidget(self.progress_bar)
def set_process(self):
''' Manipulate the ui '''
self.progress_bar.setMaximum(0)
self.progress_bar.setMaximum(10)
def update(self):
if self.i > 9:
self.timer.stop()
self.list_widget.addItem('Item ' + str(self.i))
self.progress_bar.setValue(self.i)
self.i += 1
def main():
app = QtGui.QApplication(sys.argv)
my_win = MyApp()
my_win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This example are using Qtimer object for updating progress bar with delay.
The example can be made to work quite easily by starting the processing with a single-shot timer and then calling processEvents within the loop to update the GUI:
# Execute process!
QtCore.QTimer.singleShot(100, self.process)
...
for x in range(0, 10):
time.sleep(1)
...
QtGui.qApp.processEvents(QtCore.QEventLoop.AllEvents, 50)
However, there is no guarantee that this type of approach will work with a more realistic example. You may end up needing to use threads or multiprocessing - it all depends on the specific kind of processing you are going to do.