本文實(shí)例講述了Python多線程原理與用法。分享給大家供大家參考,具體如下:
先來看個栗子:
下面來看一下I/O秘籍型的線程,舉個栗子——爬蟲,下面是爬下來的圖片用4個線程去寫文件
#!/usr/bin/env python# -*- coding:utf-8 -*-import reimport urllibimport threadingimport Queueimport timeitdef getHtml(url): html_page = urllib.urlopen(url).read() return html_page# 提取網(wǎng)頁中圖片的URLdef getUrl(html): pattern = r'src="(http://img.*?)"' # 正則表達(dá)式 imgre = re.compile(pattern) imglist = re.findall(imgre, html) # re.findall(pattern,string) 在string中尋找所有匹配成功的字符串,以列表形式返回值 return imglistclass getImg(threading.Thread): def __init__(self, queue, thread_name=0): # 線程公用一個隊(duì)列 threading.Thread.__init__(self) self.queue = queue self.thread_name = thread_name self.start() # 啟動線程 # 使用隊(duì)列實(shí)現(xiàn)進(jìn)程間通信 def run(self): global count while (True): imgurl = self.queue.get() # 調(diào)用隊(duì)列對象的get()方法從隊(duì)頭刪除并返回一個項(xiàng)目 urllib.urlretrieve(imgurl, 'E:/mnt/girls/%s.jpg' % count) count += 1 if self.queue.empty(): break self.queue.task_done() # 當(dāng)使用者線程調(diào)用 task_done() 以表示檢索了該項(xiàng)目、并完成了所有的工作時,那么未完成的任務(wù)的總數(shù)就會減少。imglist = []def main(): global imglist url = "http://huaban.com/favorite/beauty/" # 要爬的網(wǎng)頁地址 html = getHtml(url) imglist = getUrl(html)def main_1(): global count threads = [] count = 0 queue = Queue.Queue() # 將所有任務(wù)加入隊(duì)列 for img in imglist: queue.put(img) # 多線程爬去圖片 for i in range(4): thread = getImg(queue, i) threads.append(thread) # 阻塞線程,直到線程執(zhí)行完成 for thread in threads: thread.join()if __name__ == '__main__': main() t = timeit.Timer(main_1) print t.timeit(1)
4個線程的執(zhí)行耗時為:0.421320716723秒
修改一下main_1換成單線程的:
def main_1(): global count threads = [] count = 0 queue = Queue.Queue() # 將所有任務(wù)加入隊(duì)列 for img in imglist: queue.put(img) # 多線程爬去圖片 for i in range(1): thread = getImg(queue, i) threads.append(thread) # 阻塞線程,直到線程執(zhí)行完成 for thread in threads: thread.join()
單線程的執(zhí)行耗時為:1.35626623274秒

再來看一個:
#!/usr/bin/env python# -*- coding:utf-8 -*-import threadingimport timeitdef countdown(n): while n > 0: n -= 1def task1(): COUNT = 100000000 thread1 = threading.Thread(target=countdown, args=(COUNT,)) thread1.start() thread1.join()def task2(): COUNT = 100000000 thread1 = threading.Thread(target=countdown, args=(COUNT // 2,)) thread2 = threading.Thread(target=countdown, args=(COUNT // 2,)) thread1.start() thread2.start() thread1.join() thread2.join()if __name__ == '__main__': t1 = timeit.Timer(task1) print "countdown in one thread ", t1.timeit(1) t2 = timeit.Timer(task2) print "countdown in two thread ", t2.timeit(1)
新聞熱點(diǎn)
疑難解答
圖片精選