I have a pyside application which runs a function in a QThread. This function often uses print. How can I redirect the stdout to a dialog (containing a qtextedit or similar) which will display when the function is run.
Here is a minimal example:
class Main(QtGui.QWindow):
def __init__(self):
super(Main, self).__init__()
self.run_button = QtGui.QPushButton("Run")
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.run_button)
self.setLayout(mainLayout)
self.run_button.clicked.connect(self.run_event)
def run_event(self):
# Create the dialog to display output
self.viewer = OutputDialog()
# Create the worker and put it in a thread
self.worker = Worker()
self.thread = QtCore.QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.thread.start()
class Worker(QtCore.QObject):
finished = QtCore.Signal()
def run(self):
for i in range(10):
print "Im doing stuff"
time.sleep(1)
self.finished.emit()
class OutputDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(OutputDialog, self).__init__(parent)
self.text_edit = QtGui.QTextEdit()
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.text_edit)
self.setLayout(vbox)
I can modify the worker to redirect stdout to itself, and then emit the text as a signal:
class Worker(QtCore.QObject):
finished = QtCore.Signal()
message = QtCore.Signal(str)
def __init__(self):
super(Worker, self).__init__()
sys.stdout = self
def run(self):
for i in range(10):
print "Im doing stuff"
time.sleep(1)
self.finished.emit()
def write(self, text):
self.message.emit(text)
But when I connect this signal to the OutputDialog instance, the text is only displayed once the worker has finished.
I have also tried implementing the method here:
Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread
But it just causes my app to freeze.
Any ideas?
The reason your print lines only show up once the worker has finished is explained by this stack overflow answer: https://stackoverflow.com/a/20818401/1994235
To summarise, when dealing with signals/slots across threads, you need to decorate the slots with #pyqtslot(types) to make sure they are actually run in the thread you intended.
It seems like this can be done easily by subclassing QThread:
class Main(QtGui.QWindow):
def __init__(self):
super(Main, self).__init__()
self.run_button = QtGui.QPushButton("Run")
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.run_button)
self.setLayout(mainLayout)
self.run_button.clicked.connect(self.run_event)
def run_event(self):
# Create the dialog to display output
self.viewer = OutputDialog()
# Create the worker thread and run it
self.thread = WorkerThread()
self.thread.message.connect(self.viewer.write)
self.thread.start()
class WorkerThread(QtCore.QThread):
message = QtCore.Signal(str)
def run(self):
sys.stdout = self
for i in range(10):
print "Im doing stuff"
time.sleep(1)
def write(self, text):
self.message.emit(text)
However, most documentation on the web seems to recommend that you do not subclass QThread and instead use the moveToThread function for processing tasks.
Also, I don't know how you would distinguish between stdout and stderr in the above method (assuming that you redirect both to the workerthread)
Related
So I'm writing an application using PySide2 that shall redirect everything from stdout to an intermediate queue.Queue. That Queue emits a signal that will be processed by a QThread, appending all strings in the queue to a QTextEdit.
I have looked around SO quite a bit but unfortunately nothing really seems to work for me.
That's the code I'm referring to.
from PySide2 import QtWidgets, QtGui, QtCore
import sys
from queue import Queue
class WriteStream(object):
""" Redirects sys.stdout to a thread-safe Queue
Arguments:
object {object} -- base class
"""
def __init__(self, queue):
self.queue = queue
def write(self, msg):
self.queue.put(msg)
def flush(self):
""" Passing to create non-blocking stream (?!)
https://docs.python.org/3/library/io.html#io.IOBase.flush
"""
pass
class WriteStreamThread(QtCore.QThread):
queue_updated = QtCore.Signal(str)
def __init__(self, queue):
super(WriteStreamThread, self).__init__()
self.stop = False
self.queue = queue
def set_stop(self):
self.stop = True
self.wait() # waits till finished signal has been emitted
def run(self):
while not self.stop: # i guess this is blocking
msg = self.queue.get()
self.queue_updated.emit(msg)
self.sleep(1) # if commented out, app crashes
self.finished.emit()
class Verifyr(QtWidgets.QMainWindow):
def __init__(self, queue, parent=None):
super(Verifyr, self).__init__(parent)
self.centralwidget = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self.centralwidget)
self.textedit = QtWidgets.QTextEdit(self)
layout.addWidget(self.textedit)
self.setLayout(layout)
self.setCentralWidget(self.centralwidget)
print("Verifyr initialised...")
self.listener_thread = WriteStreamThread(queue)
self.listener_thread.queue_updated.connect(self._log_to_qtextedit)
self.listener_thread.start()
#QtCore.Slot(str)
def _log_to_qtextedit(self, msg):
self.textedit.insertPlainText(msg)
if __name__ == '__main__':
# create Queue to be passed to WriteStream and WriteStreamListener
queue = Queue()
# redirect stdout to WriteStream()
sys.stdout = WriteStream(queue)
print("Redirected sys.stdout to WriteStream")
# launching the app
app = QtWidgets.QApplication(sys.argv)
window = Verifyr(queue)
app.aboutToQuit.connect(window.listener_thread.set_stop)
window.show()
sys.exit(app.exec_())
In WriteStreamThread I'm using a while loop that keeps emitting signals, caught by the main application to append to its QTextEdit. When I comment out the self.sleep(1) the application ill be stuck in an infinite loop. If not the thread will exit out just fine.
Can somebody please explain this to me?
UPDATE:
like I mentioned in my comment I've found the bug... here's the updated run() method of WriteStreamThread:
def run(self):
while not self.stop:
try:
msg = self.queue.get(block=False) # nasty little kwarg
except:
msg = "No items in queue. Sleeping 1sec.."
self.sleep(1)
self.queue_updated.emit(msg)
self.finished.emit() # optional
I found the bug. It was queue.get() in the run() method of WriteStreamThread that was blocking.
Changing it to queue.get(block=False) and surrounded with try/catch does the job.
Stupid me...
I'm making an application in PyQt5 that runs a variety of long running tasks, such as scraping web pages. In order to avoid crashing the GUI, I've been using QThreads and QObjects
Currently I have a class that inherits QObject and contains a method for scraping the web pages. I move that object on to a QThread, connect a finished signal with a method that quits the thread and then start the thread.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import threading
from concurrent.futures import ThreadPoolExecutor
import requests
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.init_ui()
self.test_obj = None
self.thread = QThread(self)
def init_ui(self):
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
thread_button = QPushButton("Start Thread")
check_button = QPushButton("Check Thread")
layout.addWidget(thread_button)
layout.addWidget(check_button)
thread_button.clicked.connect(self.work)
check_button.clicked.connect(self.check)
self.setCentralWidget(widget)
self.show()
def work(self):
self.test_obj = TestObject()
self.test_obj.moveToThread(self.thread)
self.test_obj.finished.connect(self.finished)
self.thread.started.connect(self.test_obj.test)
self.thread.start()
def check(self):
for t in threading.enumerate():
print(t.name)
#pyqtSlot()
def finished(self):
self.thread.quit()
self.thread.wait()
print("Finished")
class TestObject(QObject):
finished = pyqtSignal()
def __init__(self):
super(TestObject, self).__init__()
def test(self):
with ThreadPoolExecutor() as executor:
executor.submit(self.get_url)
self.finished.emit()
def get_url(self):
res = requests.get("http://www.google.com/")
print(res)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())
Everything works as expected, I get a:
"Response [200]"
"Finished"
Printed in the console. However when I check the running threads, it shows dummy-1 thread is still running. Every time I run, it creates an additional dummy thread. Eventually I am unable to create any more threads and my application crashes.
Is it possible to use a ThreadPoolExecutor on a QThread like this? If so, is there a correct way to ensure that I don't have these dummy threads still running once I've finished my task?
I would like to have 2 worker threads in my application. One should start running as soon as the GUI is loaded and another one should start later by some signal. Let's say it's a button click.
I came across a weird behavior when my Python interpreter crashes(as in shows me Windows error "Python stopped working", no stack trace) on executing the second thread.
Here is an example, that will crash right after I click the button.
class Worker(QtCore.QThread):
def __init__(self, method_to_run):
super().__init__()
self.method = method_to_run
def run(self):
self.method()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QPushButton('Test', self)
self.label = QLabel(self)
self.button.clicked.connect(self.handleButton)
layout = QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.worker = Worker(self.test_method)
self.worker.start()
def handleButton(self):
self.label.setText('Button Clicked!')
worker = Worker(self.test_method)
worker.start()
#staticmethod
def test_method():
res = [i*i for i in range(100500)]
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
What's more weird is that it doesn't crash when you debug the application for some reason.
What am I missing here?
Edit
I could get much from the crashdump, as I don't have symbols for QT. But it looks like the crash happens inside QtCore.dll
ExceptionAddress: 00000000632d4669 (Qt5Core!QThread::start+0x0000000000000229)
The problem is that you didn't save a reference to the thread so it was deleted as soon as you exited handleButton. If you save a reference, that begs the question of how to handle its lifespan.
QThread is more than just a wrapper to a system thread - it implements other services that let you wire a thread into your GUI. You can use its finished handler to signal the widget when it terminates to do any cleanup.
In this example, I save the worker as self.worker2 and block starting the worker a second time until the first one completes.
import PyQt5
import PyQt5.QtCore as QtCore
from PyQt5.QtWidgets import *
import time
class Worker(QtCore.QThread):
def __init__(self, method_to_run):
super(Worker, self).__init__()
self.method = method_to_run
def run(self):
self.method()
class Window(QWidget):
def __init__(self):
super().__init__()
self.button = QPushButton('Test', self)
self.label = QLabel(self)
self.button.clicked.connect(self.handleButton)
layout = QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
self.worker = Worker(self.test_method)
self.worker.start()
self.worker2 = None
def handleButton(self):
self.label.setText('Button Clicked!')
# likely better to disable the button instead... but
# this shows events in action.
if self.worker2:
self.label.setText('Worker already running')
else:
self.worker2 = Worker(self.test_method)
self.worker2.finished.connect(self.handle_worker2_done)
self.worker2.start()
def handle_worker2_done(self):
self.worker2 = None
self.label.setText('Worker done')
#staticmethod
def test_method():
#res = [i*i for i in range(100500)]
time.sleep(3)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Your click triggered thread isn't actually trying to do work in a thread. You called the method, rather than passing the method as an argument, so you're trying to use the return value of test_method (None) as the method to run.
Change:
worker = Worker(self.test_method()) # Calls test_method and tries to run None
to:
worker = Worker(self.test_method) # Don't call test_method
The code runs but prints out the error: QObject::setParent: Cannot set parent, new parent is in a different thread.
What could be a reason?
import Queue, threading
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])
class MessageBox(QtGui.QMessageBox):
def __init__(self, parent=None):
QtGui.QMessageBox.__init__(self, parent)
def showMessage(self):
self.setText('Completed')
self.show()
class Thread(QtCore.QThread):
def __init__(self, queue, parent=None):
QtCore.QThread.__init__(self, parent)
self.queue=queue
def run(self):
while True:
number=self.queue.get()
result = self.process(number)
messagebox.showMessage()
self.queue.task_done()
def process(self, number):
timer = QtCore.QTimer()
for i in range(number):
print 'processing: %s'%i
QtCore.QThread.sleep(1)
return True
messagebox = MessageBox()
queue = Queue.Queue()
thread = Thread(queue)
thread.start()
lock=threading.Lock()
lock.acquire()
queue.put(3)
lock.release()
app.exec_()
In an example posted below we are reaching the widget's method using signal and slot mechanism (instead of calling it directly from the thread). The code execution works as expected. Even while I "know" the solution I would like to know why it is happening.
class Emitter(QtCore.QObject):
signal = QtCore.pyqtSignal()
class MessageBox(QtGui.QMessageBox):
def __init__(self, parent=None):
QtGui.QMessageBox.__init__(self, parent)
def showMessage(self):
self.setText('Completed')
self.show()
class Thread(QtCore.QThread):
def __init__(self, queue, parent=None):
QtCore.QThread.__init__(self, parent)
self.queue=queue
def run(self):
emitter = Emitter()
emitter.signal.connect(messagebox.showMessage)
while True:
number=self.queue.get()
result = self.process(number)
emitter.signal.emit()
self.queue.task_done()
def process(self, number):
timer = QtCore.QTimer()
for i in range(number):
print 'processing: %s'%i
QtCore.QThread.sleep(1)
return True
messagebox = MessageBox()
queue = Queue.Queue()
thread = Thread(queue)
thread.start()
lock=threading.Lock()
lock.acquire()
queue.put(3)
lock.release()
app.exec_()
You'll need to do two things. You need to move the emitter object to the second thread, and you need to declare showMessage as a slot.
emitter = Emitter()
emitter.moveToThread(self)
#QtCore.pyqtSlot()
def showMessage(self):
...
However, it's probably better to create the emitter and connect the signals and slots in the main thread and then move it to the second thread
emitter = Emitter()
emitter.signal.connect(messagebox.showMessage)
emitter.moveToThread(thread)
Also, QThreads inherit from QObject, so you don't absolutely need the emitter object, you can put the signals directly on the QThread. Just be aware that the QThread actually lives in the main thread, and any slots you have on it (except for run) are going to be executed in the main thread.
That being said, if you want to send data back and forth between the main and second thread, you may want to look into the Worker Pattern of using QThread's in Qt.
I have a thread class "MyThread" and my main application which is simply called "Gui". I want to create a few objects from the thread class but for this example I created only one object. The thread class does some work, then emits a signal to the Gui class, indicating that a user input is needed (this indication for now is simply changing the text of a button). Then the thread should wait for a user input (in this case a button click) and then continue doing what it is doing...
from PyQt4 import QtGui, QtCore
class MyTrhead(QtCore.QThread):
trigger = QtCore.pyqtSignal(str)
def run(self):
print(self.currentThreadId())
for i in range(0,10):
print("working ")
self.trigger.emit("3 + {} = ?".format(i))
#### WAIT FOR RESULT
time.sleep(1)
class Gui(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Gui, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.btn)
self.t1 = MyTrhead()
self.t1.trigger.connect(self.dispaly_message)
self.t1.start()
print("thread: {}".format(self.t1.isRunning()))
#QtCore.pyqtSlot(str)
def dispaly_message(self, mystr):
self.pushButton.setText(mystr)
def btn(self):
print("Return result to corresponding thread")
if "__main__" == __name__:
import sys
app = QtGui.QApplication(sys.argv)
m = Gui()
m.show()
sys.exit(app.exec_())
How can I wait in (multiple) threads for a user input?
By default, a QThread has an event loop that can process signals and slots. In your current implementation, you have unfortunately removed this behaviour by overriding QThread.run. If you restore it, you can get the behaviour you desire.
So if you can't override QThread.run(), how do you do threading in Qt? An alternative approach to threading is to put your code in a subclass of QObject and move that object to a standard QThread instance. You can then connect signals and slots together between the main thread and the QThread to communicate in both directions. This will allow you to implement your desired behaviour.
In the example below, I've started a worker thread which prints to the terminal, waits 2 seconds, prints again and then waits for user input. When the button is clicked, a second separate function in the worker thread runs, and prints to the terminal in the same pattern as the first time. Please note the order in which I use moveToThread() and connect the signals (as per this).
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
class MyWorker(QObject):
wait_for_input = pyqtSignal()
done = pyqtSignal()
#pyqtSlot()
def firstWork(self):
print 'doing first work'
time.sleep(2)
print 'first work done'
self.wait_for_input.emit()
#pyqtSlot()
def secondWork(self):
print 'doing second work'
time.sleep(2)
print 'second work done'
self.done.emit()
class Window(QWidget):
def __init__(self, parent = None):
super(Window, self).__init__()
self.initUi()
self.setupThread()
def initUi(self):
layout = QVBoxLayout()
self.button = QPushButton('User input')
self.button.setEnabled(False)
layout.addWidget(self.button)
self.setLayout(layout)
self.show()
#pyqtSlot()
def enableButton(self):
self.button.setEnabled(True)
#pyqtSlot()
def done(self):
self.button.setEnabled(False)
def setupThread(self):
self.thread = QThread()
self.worker = MyWorker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.firstWork)
self.button.clicked.connect(self.worker.secondWork)
self.worker.wait_for_input.connect(self.enableButton)
self.worker.done.connect(self.done)
# Start thread
self.thread.start()
if __name__ == "__main__":
app = QApplication([])
w = Window()
app.exec_()