multithreading - Python threading module error: 'NoneType' object is not callable -
my screen displays <type 'exceptions.typeerror'>: 'nonetype' object not callable
when run code below:
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 queue host = self.queue.get() #grabs urls of hosts , prints first 1024 byte of page url = urllib2.urlopen(host) #signals queue job done self.queue.task_done() start = time.time() def main(): in range(2): t = threadurl(queue) t.setdaemon(true) t.start() host in hosts: queue.put(host) queue.join() main() print "elapsed time: %s" % (time.time() - start)
this error details:
exception in thread thread-3 (most 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 file "/usr/local/lib/python2.7/threading.py", line 332, in wait <type 'exceptions.typeerror'>: 'nonetype' object not callable
what's wrong code? lot.
it's python 2.7 bug - shutdown exception in daemon thread. make threads non-deamon , pass sentinel through queue signal them exit, join them main thread.
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 queue host = self.queue.get() if host none: self.queue.task_done() return #grabs urls of hosts , prints first 1024 byte of page url = urllib2.urlopen(host) #signals queue job done self.queue.task_done() start = time.time() def main(): threads = [threadurl(queue) _ in range(2)] map(lambda t: t.start() threads) in range(2): host in hosts: queue.put(host) t in threads: queue.put(none) queue.join() map(lambda t: t.join() threads) main() print "elapsed time: %s" % (time.time() - start)
Comments
Post a Comment