python多线程问题
有人能告诉我为什么这段代码在启动线程后生成队列吗?基本上,队列是在for循环之后生成的,但在ThreadUrl类中,它已经使用queue.get()方法。这是怎么回事?它如何从尚未生成的队列中获取值python多线程问题,python,multithreading,Python,Multithreading,有人能告诉我为什么这段代码在启动线程后生成队列吗?基本上,队列是在for循环之后生成的,但在ThreadUrl类中,它已经使用queue.get()方法。这是怎么回事?它如何从尚未生成的队列中获取值 for i in range(5): t = ThreadUrl(queue, out_queue) t.setDaemon(True) t.start() # This is what confuses me! Shouldn't it be above the for
for i in range(5):
t = ThreadUrl(queue, out_queue)
t.setDaemon(True)
t.start()
# This is what confuses me! Shouldn't it be above the for loop??
for host in hosts:
queue.put(host)
for i in range(5):
dt = DatamineThread(out_queue)
dt.setDaemon(True)
dt.start()
#wait on the queue until everything has been processed
queue.join()
out_queue.join()
这是完整的来源
import Queue
import threading
import urllib2
import time
from BeautifulSoup import BeautifulSoup
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]
queue = Queue.Queue()
out_queue = Queue.Queue()
class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue, out_queue):
threading.Thread.__init__(self)
self.queue = queue
self.out_queue = out_queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and then grabs chunk of webpage
url = urllib2.urlopen(host)
chunk = url.read()
#place chunk into out queue
self.out_queue.put(chunk)
#signals to queue job is done
self.queue.task_done()
class DatamineThread(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, out_queue):
threading.Thread.__init__(self)
self.out_queue = out_queue
def run(self):
while True:
#grabs host from queue
chunk = self.out_queue.get()
#parse the chunk
soup = BeautifulSoup(chunk)
print soup.findAll(['title'])
#signals to queue job is done
self.out_queue.task_done()
start = time.time()
def main():
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadUrl(queue, out_queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
for i in range(5):
dt = DatamineThread(out_queue)
dt.setDaemon(True)
dt.start()
#wait on the queue until everything has been processed
queue.join()
out_queue.join()
main()
print "Elapsed Time: %s" % (time.time() - start)
Line
host=self.queue.get()
阻止执行线程,直到队列中出现某个元素
所以
创建等待队列中任何元素的5个线程
#populate queue with data
for host in hosts:
queue.put(host)
填满队列。在此之后,线程开始处理。Line
host=self.queue.get()
阻止执行线程,直到队列中出现某个元素
所以
创建等待队列中任何元素的5个线程
#populate queue with data
for host in hosts:
queue.put(host)
填满队列。在此之后,线程开始处理。谢谢!在循环之前和循环之后填充队列有什么不同吗?在第一个循环(创建threadURL)之后,有6个线程。主线程为队列提供数据;其他线程从该队列中消费,如果队列为空,则它们将阻塞,直到队列中出现某些内容。在创建线程之前填充队列,第一个线程将看到5个作业,第二个线程将看到4个作业,以此类推。因此每个线程都可以立即从队列中消耗。在启动线程后填充队列时,所有线程最初都会阻塞,因为队列是空的。只有在您添加一个元素后,一个线程才能从队列中获得。@奎唐,谢谢您的解释!因此,在开始和结束时填充队列基本上没有区别,因为在程序运行期间,线程将始终等待队列中任何可能的项目,对吗?@shookan You是正确的;这两种形式在您的程序中功能相同。谢谢!在循环之前和循环之后填充队列有什么不同吗?在第一个循环(创建threadURL)之后,有6个线程。主线程为队列提供数据;其他线程从该队列中消费,如果队列为空,则它们将阻塞,直到队列中出现某些内容。在创建线程之前填充队列,第一个线程将看到5个作业,第二个线程将看到4个作业,以此类推。因此每个线程都可以立即从队列中消耗。在启动线程后填充队列时,所有线程最初都会阻塞,因为队列是空的。只有在您添加一个元素后,一个线程才能从队列中获得。@奎唐,谢谢您的解释!因此,在开始和结束时填充队列基本上没有区别,因为在程序运行期间,线程将始终等待队列中任何可能的项目,对吗?@shookan You是正确的;这两种形式在程序中的功能相同。