【发布时间】:2017-10-26 06:48:43
【问题描述】:
我有一个班级 MyThread。在那,我有一个方法示例。我试图从同一个对象上下文中运行它。请看代码:
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()
看起来很简单是不是。但是当我运行它时,我得到了这个错误
AttributeError: 'myThread' object has no attribute 'sample' 现在我有了那个方法,就在那里。那么有什么问题呢?请帮忙
编辑:这是堆栈跟踪
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'
我这样称呼它
arThreads = []
maxThreads = 2;
for i in range( maxThreads ):
redisOpsObj = redisOps()
arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )
抱歉,我无法发布 redisOps 类代码。但我可以向你保证,它工作得很好
【问题讨论】:
-
会在调用堆栈中发布完整的错误吗?
-
能否请您添加您所调用的代码?
-
是否缺少某些代码。这个 sn-p 对我有用。
-
非常抱歉。是的,我的缩进被欺骗了。完全是python的新手。所以可能错过了缩进的重要性。
-
在编写 Python 代码时,您应该在代码编辑器中选择“显示制表符和空格”
标签: python python-multithreading attributeerror