国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
python多進(jìn)程并發(fā)與pool多線程

一.多進(jìn)程:

當(dāng)計(jì)算機(jī)運(yùn)行程序時(shí),就會(huì)創(chuàng)建包含代碼和狀態(tài)的進(jìn)程。這些進(jìn)程會(huì)通過計(jì)算機(jī)的一個(gè)或多個(gè)CPU執(zhí)行。不過,同一時(shí)刻每個(gè)CPU只會(huì)執(zhí)行一個(gè)進(jìn)程,然后不同進(jìn)程間快速切換,給我們一種錯(cuò)覺,感覺好像多個(gè)程序在同時(shí)進(jìn)行。例如:有一個(gè)大型工廠,該工廠負(fù)責(zé)生產(chǎn)電腦,工廠有很多的車間用來生產(chǎn)不同的電腦部件。每個(gè)車間又有很多工人互相合作共享資源來生產(chǎn)某個(gè)電腦部件。這里的工廠相當(dāng)于一個(gè)爬蟲工程,每個(gè)車間相當(dāng)于一個(gè)進(jìn)程,每個(gè)工人就相當(dāng)于線程。線程是CPU調(diào)度的基本單元。

需要注意的是單核CPU系統(tǒng)中,真正的并發(fā)是不可能的.

1.順序執(zhí)行 

2.多進(jìn)程并發(fā) 注意除了時(shí)間的加速意外也要看看函數(shù)返回值的寫法,帶有多進(jìn)程的map,是返回一個(gè)列表

  1. import requests
  2. import re
  3. import time
  4. from multiprocessing import Pool
  5. from multiprocessing.dummy import Pool as ThreadPool
  6. def spyder(url):
  7. # res = []
  8. res = {'init:':'hello'}
  9. print('hahah:{}'.format(url))
  10. time.sleep(1)
  11. # res.append(url)
  12. res.update({'entr:'+url:url})
  13. return res
  14. def use_process():
  15. urls = ["https://www.qiushibaike.com/text/page/{}/".format(str(i)) for i in range(0, 4)]
  16. start_1 = time.time()
  17. #獲取函數(shù)返回結(jié)果
  18. res1 = []
  19. for url in urls:
  20. res_ = spyder(url)
  21. res1.append(res_)
  22. end_1 = time.time()
  23. print("單進(jìn)程:", end_1 - start_1)
  24. print('res1:', res1)
  25. # 獲取函數(shù)返回結(jié)果
  26. #  進(jìn)程池
  27. start_2 = time.time()
  28. pool = Pool(processes=2)
  29. res2 = pool.map(spyder, urls)
  30. pool.close()
  31. pool.join()
  32. print('res2:', res2)
  33. end_2 = time.time()
  34. print("2進(jìn)程:", end_2 - start_2)
  35. # 獲取函數(shù)返回結(jié)果
  36. # 進(jìn)程池
  37. start_3 = time.time()
  38. pool = Pool(processes=4)
  39. res3 = pool.map(spyder, urls)
  40. pool.close()
  41. pool.join()
  42. print('res2:', res3)
  43. end_3 = time.time()
  44. print("4進(jìn)程:", end_3 - start_3)
  45. if __name__ == "__main__":
  46. use_process()

2.多線程

2.1 thread多線程

  1. import time
  2. import _thread
  3. from threading import Thread
  4. # 使用線程鎖,防止線程死鎖
  5. mutex = _thread.allocate_lock()
  6. def test(d_num):
  7. d_num.append(89)
  8. print("test: %s"% str(d_num))
  9. def test1(d_num):
  10. print("test1: %s"% str(d_num))
  11. def main():
  12. d_num = [100, 58]
  13. t1 = Thread(target=test, args=(d_num,))
  14. t2 = Thread(target=test1, args=(d_num,))
  15. t1.start()
  16. time.sleep(1)
  17. t2.start()
  18. time.sleep(1)
  19. if __name__ == '__main__':
  20. main()

2.2 多線程隊(duì)列版

  1. import time
  2. import _thread
  3. from threading import Thread
  4. import queue
  5. # 使用線程鎖,防止線程死鎖
  6. mutex = _thread.allocate_lock()
  7. frame_queue = queue.Queue()
  8. def test(d_num):
  9. print("test: %s" % str(d_num))
  10. for i in range(d_num):
  11. frame_queue.put(i)
  12. def test1():
  13. while 1:
  14. if frame_queue.empty() != True:
  15. # 從隊(duì)列中取出圖片
  16. value = frame_queue.get()
  17. print('==value:', value)
  18. time.sleep(1)
  19. else:
  20. break
  21. def main():
  22. d_num = 10
  23. t1 = Thread(target=test, args=(d_num,))
  24. t1.start()
  25. t2 = Thread(target=test1)
  26. t2.start()
  27. if __name__ == '__main__':
  28. main()

2.3 注意傳參與多進(jìn)程的區(qū)別,線程池

  1. from functools import partial
  2. from itertools import repeat
  3. from multiprocessing import Pool, freeze_support
  4. def func(a, b):
  5. return a + b
  6. def main():
  7. a_args = [1, 2, 3]
  8. second_arg = 1
  9. with Pool() as pool:
  10. L = pool.starmap(func, [(1, 1), (2, 1), (3, 1)])
  11. print('L:', L)
  12. M = pool.starmap(func, zip(a_args, repeat(second_arg)))
  13. print('M:', M)
  14. N = pool.map(partial(func, b=second_arg), a_args)
  15. print('N:', N)
  16. main()

  1. import requests
  2. import re
  3. import time
  4. from multiprocessing import Pool
  5. from multiprocessing.dummy import Pool as ThreadPool
  6. def spyder(url):
  7. # res = []
  8. res = {'init:':'hello'}
  9. print('hahah:{}'.format(url))
  10. time.sleep(1)
  11. # res.append(url)
  12. res.update({'entr:'+url:url})
  13. return res
  14. def use_process():
  15. urls = ["https://www.qiushibaike.com/text/page/{}/".format(str(i)) for i in range(0, 4)]
  16. start_1 = time.time()
  17. #獲取函數(shù)返回結(jié)果
  18. res1 = []
  19. for url in urls:
  20. res_ = spyder(url)
  21. res1.append(res_)
  22. end_1 = time.time()
  23. print("單進(jìn)程:", end_1 - start_1)
  24. print('res1:', res1)
  25. # 獲取函數(shù)返回結(jié)果
  26. #  進(jìn)程池
  27. start_2 = time.time()
  28. pool = Pool(processes=2)
  29. res2 = pool.map(spyder, urls)
  30. pool.close()
  31. pool.join()
  32. print('res2:', res2)
  33. end_2 = time.time()
  34. print("2進(jìn)程:", end_2 - start_2)
  35. # 獲取函數(shù)返回結(jié)果
  36. # 進(jìn)程池
  37. start_3 = time.time()
  38. pool = Pool(processes=4)
  39. res3 = pool.map(spyder, urls)
  40. pool.close()
  41. pool.join()
  42. print('res2:', res3)
  43. end_3 = time.time()
  44. print("4進(jìn)程:", end_3 - start_3)
  45. def use_threadpool():
  46. urls = [["https://www.qiushibaike.com/text/page/{}/".format(str(i))] for i in range(0, 4)]
  47. print('urls:', urls)
  48. # 線程池
  49. start = time.time()
  50. pool = ThreadPool(processes=4)
  51. res = pool.starmap(spyder, urls)
  52. pool.close()
  53. pool.join()
  54. end = time.time()
  55. print('res:', res)
  56. print("4線程:", end - start)
  57. if __name__ == "__main__":
  58. # use_process()
  59. use_threadpool()

實(shí)際應(yīng)用將圖片路徑和名字傳入,用zip方式打包傳參

  1. import os
  2. import cv2
  3. import time
  4. import itertools
  5. from multiprocessing.dummy import Pool as ThreadPool
  6. SIZE = (75,75)
  7. SAVE_DIRECTORY='thumbs'
  8. def save_img(filename,save_path):
  9. save_path+= filename.split('/')[-1]
  10. im = cv2.imread(filename)
  11. im=cv2.resize(im,SIZE)
  12. cv2.imwrite(save_path,im)
  13. if __name__ == '__main__':
  14. path='./data/testlabel'
  15. print(path)
  16. output_path='./data/thumbs/'
  17. if not os.path.exists(output_path):
  18. os.mkdir(output_path)
  19. print(output_path)
  20. imgs_list_path=[os.path.join(path,i) for i in os.listdir(path)]
  21. print(len(imgs_list_path))
  22. start_time=time.time()
  23. pool = ThreadPool(processes=8)
  24. print(list(zip(imgs_list_path,[output_path]*len(imgs_list_path))))
  25. pool.starmap(save_img,zip(imgs_list_path,[output_path]*len(imgs_list_path)))
  26. pool.close()
  27. pool.join()
  28. end_time=time.time()
  29. print('use time=',end_time-start_time)

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
《Python程序設(shè)計(jì)》第12章 多線程和多進(jìn)程
Python 并發(fā)編程之線程池/進(jìn)程池
使用Python下載QQ音樂歌曲
python入門系列:多進(jìn)程
python并發(fā)編程:使用多進(jìn)程multiprocessing模塊加速程序的運(yùn)行
Python多線程、多進(jìn)程和協(xié)程的實(shí)例講解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服