Python Tutorial on Multithreading

Alexander Kapps alex.kapps at web.de
Mon Feb 21 18:04:32 EST 2011


On 21.02.2011 23:30, KevinSimonson wrote:
> I've been teaching myself Python from the tutorial routed at "http://
> www.tutorialspoint.com/python/index.htm".  It's worked out pretty
> well, but when I copied its multithreading example from the bottom of
> the page at "http://www.tutorialspoint.com/python/
> python_multithreading.htm" and tried to run it I got the error
> messages:
>
> C:\Users\kvnsmnsn\Python>python mt.py
> Traceback (most recent call last):
>    File "mt.py", line 38, in<module>
>      thread = myThread(threadID, tName, workQueue)
>    File "mt.py", line 10, in __init__
>      self.name = name
>    File "C:\Python27\lib\threading.py", line 667, in name
>      assert self.__initialized, "Thread.__init__() not called"
> AssertionError: Thread.__init__() not called
>
> I don't really understand why it's giving me these messages.
> <__initialized>  gets set to<True>  when<__init__()>  gets called.
> Granted my Python program calls<__init__()>  with only one parameter,
> and the constructor in "threading.py" takes _seven_ parameters, but
> all but one have default values, so a call with just one parameter
> should be legal.  Why then is<__initialized>  getting set to<True>?
>
> My code follows.

That tutorial seems to be wrong.

According to the official docs:

"If the subclass overrides the constructor, it must make sure to 
invoke the base class constructor (Thread.__init__()) before doing 
anything else to the thread."

http://docs.python.org/library/threading.html#thread-objects

So, change your __init__ to this:

class myThread (threading.Thread):
     def __init__(self, threadID, name, q):
         threading.Thread.__init__(self)
         self.threadID = threadID
         self.name = name
         self.q = q


HTH



More information about the Python-list mailing list