[英]Python multithreading using queue
我正在閱讀有關使用Queues進行Python多線程的文章,但有一個基本問題。
基於print stmt,按預期啟動了5個線程。 那么,隊列如何工作?
1.線程最初啟動,並且在隊列中填充了某個項目時,它是否會重新啟動並開始處理該項目? 2.如果我們使用隊列系統,並且線程在隊列中逐項處理每個項目,那么性能如何提高。這是否與串行處理不同,即; 一對一
import Queue
import threading
import urllib2
import datetime
import time
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]
queue = Queue.Queue()
class ThreadUrl(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
print 'threads are created'
self.queue = queue
def run(self):
while True:
#grabs host from queue
print 'thread startting to run'
now = datetime.datetime.now()
host = self.queue.get()
#grabs urls of hosts and prints first 1024 bytes of page
url = urllib2.urlopen(host)
print 'host=%s ,threadname=%s' % (host,self.getName())
print url.read(20)
#signals to queue job is done
self.queue.task_done()
start = time.time()
if __name__ == '__main__':
#spawn a pool of threads, and pass them queue instance
print 'program start'
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
#wait on the queue until everything has been processed
queue.join()
print "Elapsed Time: %s" % (time.time() - start)
隊列與列表容器類似,但是具有內部鎖定,使其成為線程安全的數據通信方式。
啟動所有線程時發生的情況是,它們全部都阻塞了self.queue.get()
調用,等待從隊列中拉出一個項目。 當某個項目從您的主線程放入隊列時,其中一個線程將變為未阻塞狀態並接收該項目。 然后,它可以繼續處理它,直到完成並返回到阻塞狀態。
您的所有線程可以並發運行,因為它們都能夠從隊列中接收項目。 您將在這里看到性能的提高。 如果urlopen
和read
在一個線程中花費時間並且正在IO上等待,則意味着另一個線程可以工作。 隊列對象的工作僅僅是管理鎖定訪問,並彈出呼叫者項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.