subclassing Thread and start

Steve Holden sholden at
Sun Jun 22 16:39:24 CEST 2003

Note that the threading module is the one you should use for threads in your
program, the thread module is rather lower-level and therefore rather more
error prone.

Your MyThread class could look like this"

    class MyThread(threading.Thread):
        def start(self):
   = id(self)

If you *must* use the thread module, remember that although the
documentation says that you shouldn't *override* start(), that doesn't mean
you can't *extend* it. So, similar to the example aboce, you should make
sure that your thread's start_new_thread() method calls the base class'
start_net_thread() before running any of its own code.

As a general rule, you should try not to assume any knowedge of the insides
of the class you are using. Hope this helps.

Steve Holden                        
Python Web Programming       

"Gonçalo Rodrigues" <op73418 at> wrote in message
news:uk86fv060t84jqno0mniak1ol6s3nn415c at
> I have the following problem:
> In order to simplify managing Thread's, I want my (derived) Thread's
> to do two things when they start running: return their thread id to
> the thread that started them, and set an attribute, e.g., to
> the Thread id.
> Looking at the threading module code, this involves adding the
> following simple things:
> 1. Add the following to the start method:
> id = thread.start_new_thread(self.__bootstrap, ())
> return id
> 2. Add the following to the __bootstrap private method:
> = thread.get_ident()
> But in the docs, it says explicitely that when subclassing I should
> not override start, so what do you suggest? If I go ahead and override
> it, to what should I be on the lookout? Any other ideas on
> accomplishing what I need *not involving* overriding the start method?
> Hoping to have made myself understood, TIA, with my best regards,
> G. Rodrigues
> P.S: Up to now I have used the threading module patched with the above
> -- a solution i *do not like* for obvious reasons.

More information about the Python-list mailing list