Thread.__init__() not called

David Fisher python at rose164.wuh.wustl.edu
Tue Mar 14 19:44:50 EST 2000


When you sub-class Thread you need to call its __init__ function inside
yours.  Like so:

class SimpleThread(threading.Thread):
    def __init__(self,inOtherInfo):
        self.theOtherInfo = inOtherInfo
        print "setting other info to '" + self.theOtherInfo + "'"
        threading.Thread.__init__()
...

Also for future reference, if you post in html, a lot of people won't/can't
read you post, and one space is not enough indent to read you code without
extra effort.

Good luck,
David

----- Original Message -----
From: Joe Smith
Newsgroups: comp.lang.python
To: python-list at python.org
Sent: Tuesday, March 14, 2000 5:27 PM
Subject: Thread.__init__() not called



I am having the following output that I am a bit confused about.
setting other info to 'other info'
setting other info to 'other info'
Traceback (innermost last):
  File "D:\usr\karl\inproc\threadTest2.py", line 38, in ?
    TestThreads(2)
  File "D:\usr\karl\inproc\threadTest2.py", line 31, in TestThreads
    thread.start()
  File "d:\bin\Python1.5.2\Lib\threading.py", line 351, in start
    assert self.__initialized, "Thread.__init__() not called"
AssertionError: Thread.__init__() not called
Here is a small test case the causes the problem:
import time
import threading
def DoWork(numLoops, name, otherInfo):
 i = 0;
 while ( i < numLoops):
  print "hello from thread ", name, ", number ", i, ", other info is ",
otherInfo
  time.sleep(1)
  i = i + 1

class SimpleThread(threading.Thread):
 def __init__(self,inOtherInfo):
  self.theOtherInfo = inOtherInfo
  print "setting other info to '" + self.theOtherInfo + "'"
 def run(self):
  try:
   DoWork(20, self.getName(), self.theOtherInfo)
  finally:
   pass
def TestThreads(numThreads):
 try:
  o = "other info"
  threads = []
  for i in range(numThreads):
   thread = SimpleThread(o)
   threads.append(thread)
  for thread in threads:
   thread.start()
  for thread in threads:
   thread.join()
  print "done testing loggin threads."
 finally:
  del o
TestThreads(2)








More information about the Python-list mailing list