Thread for a newbie

Beeyah dbickett at gmail.com
Tue Aug 10 17:34:11 EDT 2004


justin__devine at hotmail.com (JDevine) wrote in message news:<f9d01b73.0408100642.67255b58 at posting.google.com>...
> Hi.  I have just finished my first python program. Unfortunately
> threading has me stumped.  I think this is mostly because the
> wx.thread example demo is very complex, with all the grahphics,
> graphs, and draw functions.  I want to do something very simple.  My
> program downloads files over HTTP, I want to instantiate a process
> dialog that tracks the download.  I already have access to the
> expected size of each file through other functions in my program, I
> also assume an os.foo is capable of getting the current size as the
> file grows as it is downloaded.  My issue is keeping the file
> downloading WHILE it is being tracked by the process dialog.  Please
> help with any simple thread examples you might have, if this example
> includes a dialog even better.
> Thanks for any help you can provide.
> 
> -Justin
> Justin__Devine at hotmail.com thanks

import threading # not thread

# to make your thread,
# inherit the thread.Threading class, then
# override the __init__ and run methods to define
# what your thread is actually going to do, like so:

class MyNewThread( threading.Thread ):
    def __init__( self , argument ):
        # generally speaking, the first argument of every
        # method should be self. in __init__'s case, the
        # following arguments are the values passed into the
        # thread upon creation, like so:
        # handle = MyNewThread( 1 )
        # in this case, this will define the var 'argument' as '1'
        # to make these values accessible to the other methods,
        # make it an attribute of self
        self.arg = argument
        # at the end of init you should always call the following:
        thread.Threading.__init__( self )
    def run( self ):
        # this function is called once you call MyNewThread.start()
        # once run() terminates, the thread goes with it. so just
        # define what you want your thread to do in this function.
        print repr( self.arg ) + " foo bar"
        # noteably useless

handle = MyNewThread( 2832 )
handle.start() # now the thread's run() method is running

Its important to note that, with the exception of the Queue object,
manipulating an object by the hands of multiple threads is dangerous,
and isn't recommended.

if i messed something up someone correct me, thanks.

Beeyah



More information about the Python-list mailing list