[pygtk] shutting down worker threads

Brian dol-sen at telus.net
Wed Mar 29 10:10:15 WST 2006


On Tue, 2006-28-03 at 14:38 -0600, Jason Pepas wrote:
> Hi guys,
> 
> I have been wrapping my brain around the idea of getting asynchronous gui 
> updates with worker threads working, and I have covered all but the last 
> step.
> 
> When I want to stop my program, how do I interrupt what the worker threads are 
> doing to tell them it is time to stop?
> 
> I have attached a simple example which demonstrates what I am working on.  Try 
> hitting the thread button a bunch of times and them closing the window.
> 
> Thanks,
> Jason Pepas

Here are some code snipits we use for shutting down threads.

class CommonWorker(threading.Thread):
    """ Common threading class"""
    def __init__( self ):
        """ Initialize """
        threading.Thread.__init__(self)
        # for keeping status
        self.count = 0
        # we aren't done yet
        self.done = False
        # cancelled will be set when the thread should stop
        self.cancelled = False
        # quit even if thread is still running
        self.setDaemon(1)

    def please_die( self ):
        """ Tell the thread to die and is safe to call from other threads """
        self.cancelled = True

class UpgradableListReader(CommonWorker):
    """ Read available upgrades and store them in a tuple """
    def __init__( self, installed, upgrade_only, view_prefs ):
        """ Initialize """
        CommonReader.__init__(self)
        # [snip]

    def run( self ):
        """fill upgrade tree"""
	#[snip]

	# in appropriate places such as loops add this line
	if self.cancelled: self.done = True; return


from the main thread:

	self.up_thread = UpgradableListReader(installed_list, upgrade_only_flag, preferences)
	...
	self.up_thread.please_die()
-- 
Brian <dol-sen at telus.net>



More information about the pygtk mailing list