国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多線程原理與用法實(shí)例剖析

2020-02-16 00:47:49
字體:
供稿:網(wǎng)友

本文實(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)            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐亭县| 舞钢市| 宜良县| 手机| 荣成市| 图们市| 常宁市| 阳江市| 伊吾县| 青神县| 财经| 溆浦县| 柯坪县| 图片| 乾安县| 泸定县| 常德市| 青龙| 游戏| 洪雅县| 桦川县| 合阳县| 烟台市| 隆林| 宁南县| 柯坪县| 广宗县| 东兰县| 龙山县| 阳东县| 辉县市| 德阳市| 汝州市| 红安县| 嵩明县| 莎车县| 达尔| 丹东市| 亚东县| 航空| 辽源市|