posted on: 2009-01-27 13:26:01
This is a PyQt4 implementation of a "swing worker" style thread.

>The purpose of this implementation of QThread is to run a time consuming operation and then clean up. This is a PyQt4 implementation of a "swing worker" style thread. It appears I only use one thread to implement this, but in reality the signals and slots greatly assist in this manner.

There are some limitations:

Don't create new widgets with this thread

Be careful about modifying widgets, signals and slots should still be useful in this context.

Incidentally there are some QObjects you can create. They're similar to objects you might modify.

Here is the Thread for a single action.

class Worker(QtCore.QThread):
        The idea of this thread is to run a time consuming process without shutting down
        your gui.  Whatever starts this process needs to clean it up by emitting a "cleanUp()" 
        signal is prefered.
    def __init__(self,parent,action,callback,args=[],kwargs={}):
        self.action = action
        self.args = args
        self.kwargs = kwargs = None
    def run(self):
   = self.action(*self.args,**self.kwargs)
    def cleanUp(self):
    def get(self):

This thread will execute, then tell you when it is finished, if you want the data you can get it, and then clean up the thread. If you don't want the data, just clean it up, Note: Clean it up.

I wrote a simple program to illustrate this, It calculates primes. There are some subtle differences between the Worker and MultiWorker. The multiworker is essentially a Queue, but it is also slower to be destroyed.


2009-01-30 06:52:33
Okay it has been suggested to make this using the 'multiprocess' module. Ill give it a shot, but since I use python 2.5.2 I probably won't upgrade my current apps.... yet