defget_thread(self): whileTrue: req = self.q_req.get() with self.lock: self.running += 1 try: ans = self.opener.open(req).read() except Exception, what: ans = '' print what self.q_ans.put((req, ans)) with self.lock: self.running -= 1 self.q_req.task_done() time.sleep(0.1)
if __name__ == "__main__": links = ['http://www.verycd.com/topics/%d/' % i for i in range(5420, 5430)] f = Fetcher(threads=10) for url in links: f.push(url) while f.get_task(): url, content = f.pop() print url, len(content)