I have read a lot, researched on several pages but I have not been able to find an answer to my question.
I have an application made with PyQt, this same app uploads data to a mysql database and downloads them when you tell it to. But the app works with multiple clients and I am trying to figure out how to keep each client updated with the data from the database.
I tried to make a QThread that from time to time is performing a query to see if there is new data but it hangs for some reason (I'm new to threads).
class Example(QThread):
def __init__(self)
self.bool = True
def start:
self.bool = True
while self.bool:
print("hola mundo!")
def stop:
self.bool = False
It is an example of how I am using threads, the issue is that when I call Example().start() the rest of the application hangs, it freezes and it only prints.
According to the Qt developers it is not recommended to directly derive from
QThread
and override their methods as you are doing. Although this practice is quite widespread, there are reasons to avoid it as much as possible. There are many alternatives, but perhaps the most direct is to create a subclass ofQObject
that will act as a worker and use themoveToThread
.Remember that signals can and should be used to communicate between threads and that you should never directly modify a widget from a thread . The graphical interface should only be drawn and modified from the main thread. If an interface component needs to be updated from a secondary thread, signals or any other method that is thread-safe (such as queues) will be used.
On the other hand, remember that
QThread
it doesn't run in a separate thread itself, only its methodrun
does .QThread
it's just a wrapper, be careful with this, any blocking method not called directly by willrun
block the interface.An example of a complete application using
Qthread
without using a subclass of this:The application displays an interface with two buttons and a label. One button allows to launch a thread, the other stops it. The label shows the information processed in the thread.
The process running on the thread is just a simple example, a counter from 0 to 59 with a half second wait between each number.
The process can be stopped at any time. Qt5 (pyqt5) is used but the idea is valid for Qt4 (pyqt4), only the imports would have to be changed in principle:
A lock is used to modify and access control variables on the thread. Possibly not necessary in this case which, if I'm not mistaken, are atomic operations. However, it is not bad practice to protect shared variables, memory, files, etc. between threads.
Capture of the working app:
Basically you are missing the method
run
and a__init__
, the basic architecture of a class inherited fromQThread
should be something like this:To call it from the interface class
And to connect the signals of the thread and we can process them, we connect the signal to any method, for example:
thread_start
And obviously, you have to create the method
thread_start
in the interface class, and process the messages, to inform them, or move a progress bar