Tôi có lớp MyThread. Trong đó tôi có một mẫu phương pháp. Tôi đang cố gắng để chạy nó từ withing cùng một bối cảnh đối tượng. Vui lòng xem mã:Thuộc tính PythonError: Đối tượng không có thuộc tính
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()
Có vẻ rất đơn giản phải không. Nhưng khi tôi chạy nó, tôi nhận được lỗi này
AttributeError: 'myThread' object has no attribute 'sample'
Bây giờ tôi có phương pháp đó, ngay tại đó. Vì vậy những gì là sai? Xin vui lòng giúp
Edit: Đây là 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'
Tôi gọi nó như
arThreads = []
maxThreads = 2;
for i in range(maxThreads):
redisOpsObj = redisOps()
arThreads.append(myThread(i, "Thread-"+str(i), 10, redisOpsObj))
Xin lỗi tôi không thể gửi redisOps mã lớp này. Nhưng tôi có thể đảm bảo với bạn rằng nó hoạt động tốt
Sẽ đăng lỗi hoàn chỉnh với callstack? – sloth
bạn có thể vui lòng thêm mã mà bạn đang gọi không? – Babu
Có thiếu mã nào không. Đoạn mã này hoạt động phù hợp với tôi. – ThirdOne