我有一个 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'现在我有了这个方法。那么问题出在哪里?请帮忙
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 类代码。但我可以向你保证它运行良好
这里的问题在于类的命名约定myThread。Python 区分大小写,因此myThread(小写m)和MyThread(大写M)将被视为两个不同的类。如果您在代码的不同部分使用不同的名称引用该类,则可能会导致问题。
myThread
m
MyThread
M
在你贴出的代码中,你好像定义了一个叫 的类myThread(小写的m),但是 Python 的内置threading.Thread类有一个专门用于线程执行的方法,就是run()。当这个run()方法调用时self.sample(),它好像是在类中寻找这个sample()方法myThread。
threading.Thread
run()
self.sample()
sample()
以下是解决此问题的一些建议:
这是
import threading impor import time class MyThread(threading.Thread): # Ensure consistent class name def __init__(self, threadID, name, counter, redisOpsObj): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redisOpsObj self.kill_received = threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redisOpsObj self.kill_received = threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redisOpsObj threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redisOpsObj threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redis threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOps threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redis threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = threading.Thread.__init__(self) self.threadID = threadID self.name = name self threading.Thread.__init__(self) self.threadID = threadID self.name = name threading.Thread.__init__(self) self.threadID = threadID self.name = threading.Thread.__init__(self) self.threadID = threadID threading.Thread.__init__(self) self.threadID threading.Thread.__init__(self) self threading.Thread.__init__( threading.Thread.__ threading.Thread threading False # Initialize this attribute def stop(self): self.kill_received = self.kill_receive self.ki se True def sample(self): prin print("Hello") def run(self): time.sleep( time.sleep time.s 0.1) prin print("\nStarting " + self.name) self.sample() self.sample() self.sam # Example of starting threads arThreads = [] maxThreads = arThreads = [] maxThreads arThreads = [] arThread arTh 2 for i in range(maxThreads): redisOpsObj = redisOpsObj = redisOpsOb redisOp "fakeRedisOpsObj" # Using a placeholder for redisOps object arThreads.append(MyThread(i, arThreads.append(MyThread(i, arThreads.append(MyThread arThreads "Thread-" + str(i), 10, redisOpsObj)) for thread in arThreads: thread.start() thread.start() `` thread.start() thread.start thread
self.kill_received = False
AttributeError``stop
这应该可以解决问题。确保您的实际类名和方法名匹配,这样您就不会再遇到AttributeError。
AttributeError