Mam klasę MyThread. W tym mam próbkę metody. Próbuję uruchomić go z tym samym kontekście obiektu. Proszę spojrzeć na kod:Python AttributeError: Obiekt nie ma atrybutu
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, redisOpsObj):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
self.redisOpsObj = redisOpsObj
def stop(self):
self.kill_received = True
def sample(self):
print "Hello"
def run(self):
time.sleep(0.1)
print "\n Starting " + self.name
self.sample()
Wygląda bardzo prosto, nie jest. Ale kiedy go uruchomię, pojawia się ten błąd. Teraz mam tę metodę, właśnie tutaj. Co jest nie tak? Proszę o pomoc
Edit: To jest StackTrace
Starting Thread-0
Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
Wołam go jak ten
arThreads = []
maxThreads = 2;
for i in range(maxThreads):
redisOpsObj = redisOps()
arThreads.append(myThread(i, "Thread-"+str(i), 10, redisOpsObj))
Niestety nie mogę zakładać redisOps kod klasy. Ale zapewniam cię, że to działa dobrze
Czy zostałby przesłany kompletny błąd do pliku wywoławczego? – sloth
Czy mógłbyś dodać kod, w jaki sposób dzwonisz? – Babu
Czy brakuje jakiegoś kodu. Ten fragment działa dla mnie fien. – ThirdOne