James Thomas James Thomas - 5 months ago 35
Python Question

Single worker thread for all tasks or multiple specific workers?

I'm creating a simple GUI application using PyQt5 where I request some data from an API which is then used to populate various controls of the UI.

The examples I was following about worker threads in PyQt all seem to sub-class

QThread
and then do their business logic in the overridden
run()
method. This works fine but I want to execute different API calls at different times using a worker.

So my question is: do I need to create a specific worker thread for every operation I wish to do or is there a way of having a single thread class that I can use to carry out different operations at different times and therefore avoid the overhead of creating different thread sub-classes?

Answer

What you can do is design an object to do all these tasks (inherit QObject for slots / signals). Lets say each task is defined as a separate function - lets designate these functions as slots.

Then (a general order of events):

  • instantiate a QThread object.
  • instantiate your class.
  • Move your object into the thread using YouClass->moveToThread(pThread).
  • Now define a signal for each slot and connect these signals to the relevant slots in your object.
  • Finally run the thread using pThread->start()

Now you can emit a signal to do a particular task in the thread. You do not need to sub-class QThread just use a normal class derived from QObject (so that you can use slots/signals).

You can either use one class in one thread to do many operations (note: they will be queued). Or make many classes in many threads (to run "parallel").

I don't know python well enough to attempt an example here so I won't :o

Note: The reason to sub-class QThread would be if you wanted to extend the functionality of the QThread class - i.e. add more/specific thread-related functions. QThread is a class that controls a thread, and is not meant to be used to run arbitrary/generic tasks... even though you can abuse it to do so if you wish :)