Skip to content Skip to sidebar Skip to footer

Migrating From Inherited Qthread To Worker Model

So through a lot of help in my previous questions (Interrupting QThread sleep and PySide passing signals from QThread to a slot in another QThread) I decided to attempt to change f

Solution 1:

The first issue is that you need to connect your myObject.do_work method to QThread.started:

self.myRunThread.started.connect(self.myRunObj.do_work)

Secondly, your do_work method should include something along these lines to enable event processing (please forgive my rusty PyQt and pseudocode):

defdo_work(self):
    while someCondition:
        #The next two lines are critical for events and queued signalsif self.thread().eventDispatcher().hasPendingEvents():
            self.thread().eventDispatcher().processEvents(QEventLoop.AllEvents)
        ifnot self.meetsSomeConditionToContinueRunning():
            breakelif self.hasWorkOfSomeKind():
            self.do_something_here()
        else:
            QThread.yieldCurrentThread()

For more on this, check out the docs for QAbstractEventDispatcher.

The logic here is that when a signal is emitted from one thread (myWidget.datagramHandled), it gets queued in your worker thread's event loop. Calling processEvents processes any pending events (including queued signals, which are really just events), invoking the appropriate slots for any queued signals (myRunObj.packet_handled).

Further reading:

Solution 2:

There 3 possible ways of distributing the computation/other load with Qt:

  1. Explicitly putting the load to concrete QThread instance. That is thread-based concurrency.
  2. Implicitly putting the load to pooled QThread instance. That is closer to task-based concurrency yet 'manually' managed with your own logic. QThreadPool class is used for maintaining the pool of threads.
  3. Starting the task in own threading context we never explicitly manage. That is task-based concurrency and QtConcurrent namespace used. My guess is that task-based concurrency and "worker model" is the same thing (observed your changes). Mind that QtConcurrent does offer parallelization for tasks and uses exceptions (which may affect the way you write the code) unlike the rest of Qt.

Given you use PyQt you can also take an advantage of the feature designated for the pattern you want to implement with QtConcurrent for PyQt.

P.S. I see use thread.sleep( interval ) and that is not a good practice and one more indication that the proper technique should be used for implementing 'Worker model'.

Solution 3:

An alternative to the solution provided by @JonHarper is to replace your while loop with a QTimer. Because you have an event loop running in your worker process now, it can handle QTimer events correctly (as long as you construct the QTimer in the relevant thread).

This way, control is returned to the event loop periodically so that other slots can be run when required.

Post a Comment for "Migrating From Inherited Qthread To Worker Model"