One workflow for all tasks or several specific workers?

I am creating a simple GUI application using PyQt5 where I request some data from the API, which is then used to populate various user interface controls.

The examples I ran about workflows in PyQt seem to be a subclass of QThread , and then execute their business logic in an overridden run() method. This works fine, but I want to make different API calls at different times using a working one.

So my question is: do I need to create a specific workflow for each operation that I want to do, or is there a way to have one thread class that I can use to perform different operations at different times and therefore avoid the overhead of creating different subclasses threads?

+5
source share
2 answers

What you can do is create an object to perform all these tasks (inherit QObject for slots / signals). Suppose each task is defined as a separate function - it allows you to assign these functions to slots.

Then (general order of events):

  • create an instance of the QThread object.
  • create an instance of the class.
  • Move the object to the stream using YouClass->moveToThread(pThread) .
  • Now define a signal for each slot and connect these signals to the corresponding slots in your facility.
  • Finally, start the thread using pThread->start()

Now you can emit a signal to perform a specific task in the stream. You do not need the QThread subclass to use a regular class derived from QObject (so you can use slots / signals).

You can use one class in one thread to perform many operations (note: they will be queued). Or to make many classes in many flows (for start "in parallel").

I don't know python well enough to try the example here, so I won’t: o

Note. The reason for the QThread subclass would be if you wanted to extend the functionality of the QThread class, i.e. add additional / stream related functions. QThread is a class that controls the thread and is not intended to use arbitrary / common tasks ... even if you can abuse it if you want :)

+7
source

Here is a brief example of one (but possibly as large as possible) work object that moves to a single running QThread (running) and exchanges data through signals. Also the end stops at the end. He demonstrates that code_fodder is outlined in his answer .

 from PyQt4 import QtCore QtCore.Signal = QtCore.pyqtSignal class Master(QtCore.QObject): command = QtCore.Signal(str) def __init__(self): super().__init__() class Worker(QtCore.QObject): def __init__(self): super().__init__() def do_something(self, text): print('current thread id = {}, message to worker = {}'.format(int(QtCore.QThread.currentThreadId()), text)) if __name__ == '__main__': app = QtCore.QCoreApplication([]) # give us a thread and start it thread = QtCore.QThread() thread.start() # create a worker and move it to our extra thread worker = Worker() worker.moveToThread(thread) # create a master object and connect it to the worker master = Master() master.command.connect(worker.do_something) # call a method of the worker directly (will be executed in the actual thread) worker.do_something('wrong way to communicate with worker') # communicate via signals, will execute the method now in the extra thread master.command.emit('right way to communicate with worker') # start the application and kill it after 1 second QtCore.QTimer.singleShot(1000, app.quit) app.exec_() # don't forget to terminate the extra thread thread.quit() thread.wait(5000) 
+3
source

All Articles