3
我的屏幕显示<type 'exceptions.TypeError'>: 'NoneType' object is not callable
当我运行下面这段代码:Python的线程模块错误:“NoneType”对象不是可调用
import threading
import urllib2
import Queue
import time
hosts = ["http://baidu.com", "http://yahoo.com"]
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
""" Threaded Url Grab """
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and prints first 1024 byte of page
url = urllib2.urlopen(host)
#signals to queue job is done
self.queue.task_done()
start = time.time()
def main():
for i in range(2):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
for host in hosts:
queue.put(host)
queue.join()
main()
print "Elapsed Time: %s" % (time.time() - start)
这是错误的详细信息:
Exception in thread Thread-3 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 808, in __bootstrap_inner
File "url_thread.py", line 21, in run
File "/usr/local/lib/python2.7/Queue.py", line 168, in get
File "/usr/local/lib/python2.7/threading.py", line 332, in wait
<type 'exceptions.TypeError'>: 'NoneType' object is not callable
这有什么错我的代码?非常感谢。
使用'超(ThreadUrl,个体经营).__的init __()'比其他'threading.Thread .__的init __(个体经营)建议',看到http://stackoverflow.com/questions/576169/understanding-python -super-and-init-methods – shuiyu
感谢您的评论。但它发生同样的错误。我的系统上的 – changzhi
(win 7 x64; python 2.7.1 32bit你的代码工作正常 –