Blocking QThread blocks GUI - python

For a simple chat program i use a c lib that is wrapped via boost::python.
A simple GUI is written using PyQT. Receiving messages is done via a blocking call to
said lib. For the GUI to refresh independently the communication part is in a QThread.
While i would assume the GUI and the communication to be independent, the GUI is extremely unresponsive and seems to only update when messages are coming in.
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import pynetcom2
import time
class NetCom(QThread):
def __init__(self):
QThread.__init__(self)
self.client = pynetcom2.Client()
self.client.init('127.0.0.1', 4028)
self.client.provide('myChat', 1)
self.client.subscribe('myChat', 100)
def run(self):
while (1):
print "Waiting for message..."
text = self.client.recvStr('myChat', True)
return
class Netchat(QMainWindow):
def __init__(self, argv):
if (len(argv) != 2):
print "Usage: %s <nickname>" %(argv[0])
sys.exit(1)
self.nickname = argv[1]
print "Logging in with nickname '%s'" %(self.nickname)
super(Netchat, self).__init__()
self.setupUI()
rect = QApplication.desktop().availableGeometry()
self.resize(int(rect.width() * 0.3), int(rect.height() * 0.6))
self.show()
self.com = NetCom()
self.com.start()
def setupUI(self):
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.testList = QListWidget()
mainLayout = QHBoxLayout()
mainLayout.addWidget(self.testList)
centralWidget.setLayout(mainLayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
netchat = Netchat(sys.argv)
app.exec_()

This might be caused by the infamous Global Interpreter Lock (GIL). Python does not allow two threads to execute Python code at the same time. In your C function, you have to explicitly release and re-acquire the GIL if you want your GUI code to run in parallel.
This is explained in the Python C API documentation: Thread State and the Global Interpreter Lock.
It boils down to using the following macros in your C extension:
Py_BEGIN_ALLOW_THREADS
// Your expensive computation goes here.
Py_END_ALLOW_THREADS

Related

How can I 'merge' my main program with a GUI?

Just to make a clear distinction between my questions and a lot of other questions on here:
I have already written the 'main' program (which has classes, functions, and variables alike) and a good chunk of the GUI.
So this isn't a question on how to write in tkinter or python, but more so how can I combine them?
Should I run the program from the GUI? And import the various variables, functions, and classes? And if so, should I import the whole main program of use from to import each item when needed?
Should I create a third program that imports the main and the GUI?
I just can't seem to find any clear answer, or at least I can't seem to find out how to even phrase the question because all search results point to how to write GUI, which I already get the gist of.
Here is an example of structure I did for one of my projects containing a Server (your actual main code), a GUI, and a third program I called "App" that just runs the 2. I created functions like link_with_gui or link_with_server so you can access to your GUI's variables from the Server and vice-versa.
To run this program, you just have to call python app.py. I added sections if __name__ == '__main__' in the Server and GUI so you can run them independantly (for testing purposes).
EDIT : I updated my code with threads. In the Server, you have an infinite loop that increments the variable self.count every second, and in the GUI if you click on the button, it will print this count.
App :
# app.py
from server import Server
from gui import GUI
class App:
def __init__(self):
self.gui = GUI()
self.server = Server()
self.link_parts()
def link_parts(self):
self.server.link_with_gui(self.gui)
self.gui.link_with_server(self.server)
def main():
app = App()
app.gui.mainloop()
if __name__ == '__main__':
main()
Server :
# server.py
import threading
import time
class Server:
def __init__(self):
self.count = 0
thread = threading.Thread(target=self.counter)
thread.daemon = True
thread.start()
def link_with_gui(self, gui):
self.gui = gui
def display(self):
self.gui.chat_text.delete("1.0", "end")
self.gui.chat_text.insert("insert", "This is Server count : {}".format(self.count))
def counter(self):
while True:
self.count += 1
time.sleep(1)
print("self.count", self.count)
if __name__ == '__main__':
server = Server()
time.sleep(4)
GUI :
# gui.py
import tkinter as tk
class GUI(tk.Tk): # Graphic User Interface
def __init__(self):
super().__init__()
self.btn = tk.Button(master=self, text="Click Me")
self.btn.pack()
self.chat_text = tk.Text(self, width=20, height=3)
self.chat_text.pack()
def link_with_server(self, server):
self.server = server
self.btn.configure(command=self.server.display)
if __name__ == '__main__':
gui = GUI()
gui.mainloop()

Display PyQt5 widget from background process

I want to write a python program run on background, and displaying PyQt5 GUI from background process on neccessary.
My solution is use RabbitMQ to do IPC work. program start with PyQt run on main thread, and start a thread listening RabbitMQ to show GUI on call.
Here is the code:
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import QThreadPool, QObject, QRunnable, pyqtSignal
import traceback
import pika
import sys
class RabbitMQSignals(QObject):
target = pyqtSignal(int)
class MessageListener(QRunnable):
def __init__(self):
super(MessageListener, self).__init__()
self.signals = RabbitMQSignals()
def run(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
self.channel = self.connection.channel()
self.channel.queue_declare(queue='ui')
self.channel.basic_consume(queue='ui', on_message_callback=self.dispatch, auto_ack=True)
print('Waiting for signals')
self.channel.start_consuming()
def dispatch(self, channel, method, properties, body):
body = body.decode('utf-8')
if body == 'quit':
sys.exit(0)
print('[x] Received %s' % body)
self.signals.target.emit(0)
class MainWidget(QObject):
def __init__(self):
super(MainWidget, self).__init__()
def show(self, action):
try:
print('[x] Dispatched :' + str(action))
label = QLabel('Hello World')
label.show()
except:
print(traceback.format_exc())
if __name__ == '__main__':
app = QApplication([])
widget = MainWidget()
pool = QThreadPool()
listener = MessageListener()
listener.signals.target.connect(widget.show)
pool.start(listener)
app.exec_()
Now, everything works fine except that the label.show line crashes the program, no widget displayed, no message printed.
The client part is listed below, sending quit to quit the server.
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='ui')
channel.basic_publish(exchange='', routing_key='ui', body='show label')
connection.close()
My question is, how and why the label.show() shutdown the program without any error? How can I improve the program?
Or is there alternative ways to do the job?
Any suggestions are welcome.
Thank you in advance.
The problem is that QLabel is a local variable so it will be deleted a moment after the show method is executed. The above should not cause the application to terminate but by default QApplication will be closed if at any time after showing at least one window they all close, in your case QLabel is displayed for a moment and closes, which consequently implies that the application it closes.
So the solution is to make the label a member of the class so that it is not deleted:
# ...
class MainWidget(QObject):
def __init__(self):
super(MainWidget, self).__init__()
self.label = QLabel("Hello World")
def show(self, action):
print('[x] Dispatched :' + str(action))
self.label.show()
# ...
On the other hand, Qt does not return exceptions when some function/method fails in its task, instead it informs us of the success of the execution through a variable, this does it for reasons of efficiency. And that same PyQt inherits so in 99.99% of the python code is not necessary to use try-except.

How to create PyQt5 GUI that does not occupy the main thread

Is it possible to get Python to launch a PyQt5 GUI application using the main thread of execution, and then leave the thread open to do other things?
Here is the simplest example I can think of:
from PyQt5.QtWidgets import *
def infinite_useless_loop():
while True:
pass
app = QApplication([])
doodad = QMainWindow()
doodad.show()
infinite_useless_loop()
If you run this code, it freezes because the 'mainloop' is not activated as it normally would be if I didn't make a call to the useless infinite loop, and instead put a call to app.exec_(). The call to infinite_useless_loop is meant to substitute the main thread being used to do other useful work WHILE the GUI is running correctly.
To do this correctly, I'd have to imagine one would make a separate thread and use that to run the GUI, while launching that thread with the main thread, and just continuing on from there; able to communicate if necessary in the future.
EDIT
I wrote some code that gets the main event loop working in a seperate thread, but I am still not able to fully interact with the GUI object that is created in another thread. This code effectively solves half of my problem:
from PyQt5.QtWidgets import *
from threading import Thread
import sys
def makeGUI():
app = QApplication([])
doodad = QMainWindow()
doodad.show()
sys.exit(app.exec_())
def infinite_useless_loop():
while True:
pass
if __name__ == '__main__':
thread = Thread(target=makeGUI)
thread.start()
infinite_useless_loop()
This code spawns the blank window, able to be clicked on and resized correctly (because the event loop is working), but is on its own. Do I have to revert to signals and slots to communicate with the object, or can I somehow get the GUI object back into the main thread and use it's methods from there?
EDIT 2
I do not wish to do any actual GUI manipulation outside of the GUI's thread, just call methods on the GUI object that I HAVE MADE. The entire GUI structure would be made by the GUI object.
EDIT 3
This new code now makes it able to be communicated with, but only with raw bits of information, and not by one thread accessing another thread's GUI object's methods. This is close, but its still not what I want. Calling the GUI object's methods would have to be done by passing the raw information through the queue, and then being processed at the other side. Then an exec() function would have to be called. That is not acceptable. Here is the code:
from PyQt5.QtWidgets import *
import multiprocessing
import threading
import sys
import time
import queue
class Application(QMainWindow):
def __init__(self, comms):
super(Application, self).__init__()
self.comms = comms
self.fetchingThread = threading.Thread(target=self.fetchingLoop)
self.labelOne = QLabel(self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.labelOne)
self.setLayout(self.layout)
self.fetchingThread.start()
def fetchingLoop(self):
while True:
try:
obj = self.comms.get()
self.processItem(obj)
except queue.Empty:
pass
def processItem(self, item):
self.labelOne.setText(str(item))
print(self.labelOne.text())
class Launcher(multiprocessing.Process):
def __init__(self, Application):
super(Launcher, self).__init__()
self.comms = multiprocessing.Queue()
self.Application = Application
def get_comms(self):
return self.comms
def run(self):
app = QApplication([])
window = self.Application(self.comms)
window.show()
sys.exit(app.exec_())
def no_thread():
app = QApplication([])
window = Application(False)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
thread = Launcher(Application)
comms = thread.get_comms()
thread.start()
c = 0
while True:
c += 1
time.sleep(1)
comms.put(c)

How to display progress without multi-threading

Let's say I have a PyQt program that goes through a given directory, looks for *JPEG images, and does some processing every time it finds one. Depending on the size of the selected directory, this may take from some seconds to minutes.
I would like to keep my user updated with the status - preferably with something like "x files processed out of y files" . If not, a simple running pulse progress bar by setting progressbar.setRange(0,0) works too.
From my understanding, in order to prevent my GUI from freezing, I will need a seperate thread that process the images, and the original thread that updates the GUI every interval.
But I am wondering if there is any possible way for me to do both in the same thread?
Yes, you can easily do this using processEvents, which is provided for this exact purpose.
I have used this technique for implementing a simple find-in-files dialog box. All you need to do is launch the function that processes the files with a single-shot timer, and then periodically call processEvents in the loop. This is should be good enough to update a counter with the number of files processed, and also allow the user to cancel the process, if necessary.
The only real issue is deciding on how frequently to call processEvents. The more often you call it, the more responsive the GUI will be - but this comes at the cost of considerably slowing the processing of the files. So you may have to experiment a little bit in order to find an acceptable compromise.
UPDATE:
Here's a simple demo that shows how the code could be structured:
import sys, time
from PyQt5 import QtWidgets, QtCore
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.button = QtWidgets.QPushButton('Start')
self.progress = QtWidgets.QLabel('0')
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.button)
layout.addWidget(self.progress)
self.button.clicked.connect(self.test)
self._stop = False
self._stopped = True
def test(self):
if self._stopped:
self._stop = False
self.progress.setText('0')
self.button.setText('Stop')
QtCore.QTimer.singleShot(1, self.process)
else:
self._stop = True
def process(self):
self._stopped = False
for index in range(1, 1000):
time.sleep(0.01)
self.progress.setText(str(index))
if not index % 20:
QtWidgets.qApp.processEvents(
QtCore.QEventLoop.AllEvents, 50)
if self._stop:
break
self._stopped = True
self.button.setText('Start')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I could not achieve the thing you need without multi threading and this is not possible because gui can be only updated in main thread. Below is an algorithm how I did this with multithreading.
Let's say you have your application processing images. Then there are the following threads:
Main thread (that blocks by GUI/QApplication-derived classes.exec())
Timer with, for example, 1 second interval which updates a variable and calls a slot in GUI thread which updates a variable in user interface.
A thread which is processing images on your pc.
def process(self):
self._status = "processing image 1"
....
def _update(self):
self.status_label.setText(self._status)
def start_processing(self, image_path):
# create thread for process and run it
# create thread for updating by using QtCore.QTimer()
# connect qtimer triggered signal to and `self._update()` slot
# connect image processing thread (use connect signal to any slot, in this example I'll stop timer after processing thread finishes)
#pyqtSlot()
def _stop_timer():
self._qtimer.stop()
self._qtimer = None
_update_thread.finished.connect(_stop_timer)
In pyqt5 it is possible to assign a pyqtvariable from a one nested thread(first level). So you can make your variable a pyqtvariable with setter and getter and update gui in a setter or think how you can do this by yourself.
You could just use the python threading module and emit a signal in your threaded routine.
Here's a working example
from PyQt4 import QtGui, QtCore
import threading
import time
class MyWidget(QtGui.QWidget):
valueChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.computeButton = QtGui.QPushButton("Compute", self)
self.progressBar = QtGui.QProgressBar()
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.computeButton)
layout.addWidget(self.progressBar)
self.computeButton.clicked.connect(self.compute)
self.valueChanged.connect(self.progressBar.setValue)
def compute(self):
nbFiles = 10
self.progressBar.setRange(0, nbFiles)
def inner():
for i in range(1, nbFiles+1):
time.sleep(0.5) # Process Image
self.valueChanged.emit(i) # Notify progress
self.thread = threading.Thread(target = inner)
self.thread.start()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

Communicate from Main Application Thread to Seperate QThread

I have this basic app working. It creates a new thread, and starts it. Then it uses signals to communicate back to the main thread for something else to happen.
My question is how do I pass data from the main thread to the new thread that is created, this part really doesn't make sense to me. Or is there another way altogether to do threading back and forth. Essentially the main thread and the new thread will run for the entire life of the application, so they need to communicate back and forth.
As a note I am a web developer so native apps are new to me. Also I am still new to qt and pyqt so not sure how to do this.
import sys
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, pyqtSignal
class Thread(QThread):
message_recieved = pyqtSignal(object)
def run(self):
self.message_recieved.emit('hello')
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self,parent)
self.initUI()
self.thread = Thread()
self.thread.message_recieved.connect(self.message)
self.thread.start()
def message(self, msg):
print msg
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("Test App")
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
You shouldn't subclass QThread. You should have a worker sent on the thread you created.
Have a look at this link to get best practices regarding threading in Qt:
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
The example is in C++, but can be easily translated to Python.
Good luck!

Categories

Resources