Simple Thread Program not working (Newbie: Python)

Jp Calderone kuran42 at yahoo.com
Sat Nov 10 07:17:18 EST 2001


Take a close look at your __init__ declarations :)


karthik Guru wrote:

> hello all,
> Am not sure why am getting the errors at the specified place ( I have pasted
> the error on those lines)
> Can someone help.
> ---------------
> 
> import threading, time
> 
> class Test:
>     def __init__(self):
>         self.data = "start"
>     def change(self,data):
>         self.data = data
>     def __str__(self):
>         return self.data
>     def __repr__(self):
>         return self.data
> 
> class ChangeThread(threading.Thread):
>     def __init_(self,val,a):
>         self.ref = a
>         threading.Thread.__init__(None,self)
>     def run(self):
>         count = 1
>         while 1:
>             self.ref.change(s,count) # AttributeError: ChangeThread instance
> has no attribute 'ref'..this is the error.
>             count+=1
>             time.sleep(1)
> 
> class WatchThread(threading.Thread):
>     def __init_(self,val,a):
>         self.ref = a
>         threading.Thread.__init__(None,self)
>     def run(self):
>         while 1:
>             print 'changed data : ' + self.ref.data   # AttributeError:
> ChangeThread instance has no attribute 'ref'
>             time.sleep(3)
> 
> 
> def execute():
>     print 'Start execute'
>     t = Test()
>     t1 = WatchThread(None,t)
>     t2 = WatchThread(None,t)
>     t3 = WatchThread(None,t)
>     t1.start()
>     t2.start()
>     t3.start()
>     time.sleep(2)
> 
>     t4 = ChangeThread(None,t)
>     t4.start()
> 
> if __name__ == '__main__':
>     execute()
> 
> 
> 
> This is the trace:
> 
> Start execute
> Exception in thread Thread-1:
> Traceback (most recent call last):
>   File "d:\python21\lib\threading.py", line 378, in __bootstrap
>     self.run()
>   File "thread1.py", line 30, in run
>     print 'changed data : ' + self.ref.data
> AttributeError: WatchThread instance has no attribute 'ref'
> 
> Exception in thread Thread-2:
> Traceback (most recent call last):
>   File "d:\python21\lib\threading.py", line 378, in __bootstrap
>     self.run()
>   File "thread1.py", line 30, in run
>     print 'changed data : ' + self.ref.data
> AttributeError: WatchThread instance has no attribute 'ref'
> 
> Exception in thread Thread-3:
> Traceback (most recent call last):
>   File "d:\python21\lib\threading.py", line 378, in __bootstrap
>     self.run()
>   File "thread1.py", line 30, in run
>     print 'changed data : ' + self.ref.data
> AttributeError: WatchThread instance has no attribute 'ref'
> 
> Exception in thread Thread-4:
> Traceback (most recent call last):
>   File "d:\python21\lib\threading.py", line 378, in __bootstrap
>     self.run()
>   File "thread1.py", line 20, in run
>     self.ref.change(s,count)
> AttributeError: ChangeThread instance has no attribute 'ref'
> 
> 
> 
> 




More information about the Python-list mailing list