Python multiprocessing Pool map()インスタンス


理由:あるディレクトリの下のファイルの文字数と行数を並列に処理し、res.txtファイルに格納する
# coding=utf-8
import os
import time
import logging
from multiprocessing import Pool

x1, x2 = 3, 7
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [*] %(message)s"
)


def getFile (path):
    #         list
    fileList = []
    for root, dirs, files in list(os.walk(path)):
        for i in files:
            if i.endswith('.txt') or i.endswith('.py'):
                file = str(root + "\\" + i).replace("\\", "/")
                fileList.append(file)
                logging.info(file)
    return fileList


def operFile (filePath_):
    #              ,   
    filePath = filePath_.replace("/", "\\")
    with open(filePath, "r", encoding="utf-8") as fp:
        content = fp.readlines()
        lines = len(content)  #   
        alphaNum = 0  #   
        for i in content:
            alphaNum += len(i.strip('
'
)) return lines, alphaNum, filePath def out (list1, writeFilePath): # # fileLines = 0 charNum = 0 with open(writeFilePath, 'w', encoding="utf-8")as fp: for i in list1: fp.write(f" :{str(i[0])} \t :{str(i[1])} \t :{i[2]}
"
) fileLines += i[0] charNum += i[1] fp.write(f" :{fileLines} \t :{charNum}") if __name__ == "__main__": # startTime = time.time() fileList = getFile(r"F:\python") pool = Pool(5) resultList = pool.map(operFile, fileList) # pool.close() pool.join() out(resultList, r"F:\python\res.txt") endTime = time.time() logging.info(f"used time is {endTime - startTime}")

出力:
2019-10-03 21:09:34,917 [*] F:/python/1.py
2019-10-03 21:09:34,918 [*] F:/python/1.txt
2019-10-03 21:09:34,918 [*] F:/python/Barrier  .py
2019-10-03 21:09:34,918 [*] F:/python/condition    .py
2019-10-03 21:09:34,918 [*] F:/python/debug   .py
2019-10-03 21:09:34,918 [*] F:/python/res.txt
2019-10-03 21:09:34,918 [*] F:/python/semaphore   .py
2019-10-03 21:09:34,918 [*] F:/python/test.py
2019-10-03 21:09:34,918 [*] F:/python/threading    .py
2019-10-03 21:09:34,918 [*] F:/python/translate  .py
2019-10-03 21:09:34,918 [*] F:/python/          .py
2019-10-03 21:09:34,918 [*] F:/python/     .py
2019-10-03 21:09:34,918 [*] F:/python/        .py
2019-10-03 21:09:34,918 [*] F:/python/dome/models.py
2019-10-03 21:09:34,918 [*] F:/python/dome/session.py
2019-10-03 21:09:34,918 [*] F:/python/dome/spider.py
2019-10-03 21:09:34,919 [*] F:/python/Python    /1. Python     (      ).py
2019-10-03 21:09:34,919 [*] F:/python/Python    /10. threading-Python Queue        .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /11. threading-Python Event      .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /12. threading-Python          (    ).py
2019-10-03 21:09:34,919 [*] F:/python/Python    /13. threading-Python threading Local().py
2019-10-03 21:09:34,919 [*] F:/python/Python    /14. threading-Python Timer   :           .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /15. Python schedule        .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /16. Python os.fork().py
2019-10-03 21:09:34,919 [*] F:/python/Python    /17. multiprocessing-Python Process    (2.py
2019-10-03 21:09:34,919 [*] F:/python/Python    /18. multiprocessing-Python       3   .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /19.               .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /2. Python    (2.py
2019-10-03 21:09:34,919 [*] F:/python/Python    /20. multiprocessing-Python         .py
2019-10-03 21:09:34,919 [*] F:/python/Python    /21. multiprocessing-Python      2     (Queue Pipe).py
2019-10-03 21:09:34,920 [*] F:/python/Python    /22. Python Futures      .py
2019-10-03 21:09:34,920 [*] F:/python/Python    /23. Python Asyncio      .py
2019-10-03 21:09:34,920 [*] F:/python/Python    /24. Python GIL        (    ).py
2019-10-03 21:09:34,920 [*] F:/python/Python    /25.     Python      (    ).py
2019-10-03 21:09:34,920 [*] F:/python/Python    /3. Python       (  、  、  、     ).py
2019-10-03 21:09:34,920 [*] F:/python/Python    /4. threading-Python Thread join()    .py
2019-10-03 21:09:34,920 [*] F:/python/Python    /5. threading-Python       (  2.py
2019-10-03 21:09:34,920 [*] F:/python/Python    /6. threading-Python sleep().py
2019-10-03 21:09:34,920 [*] F:/python/Python    /7. threading-Python   (Lock):         .py
2019-10-03 21:09:34,921 [*] F:/python/Python    /8. threading-     ,      (4.py
2019-10-03 21:09:34,921 [*] F:/python/Python    /9. threading-Python condition      (   ).py
2019-10-03 21:09:34,921 [*] F:/python/Python    /  .txt
2019-10-03 21:09:35,261 [*] used time is 0.3469226360321045

res.txtファイル:
601542 	    :F:\python\1.py 
  :83426082 	    :F:\python\1.txt 
  :471184 	    :F:\python\Barrier  .py 
  :601368 	    :F:\python\condition    .py 
  :521225 	    :F:\python\debug   .py 
  :432773 	    :F:\python\res.txt 
  :34785 	    :F:\python\semaphore   .py 
  :114 	    :F:\python\test.py 
  :39992 	    :F:\python\threading    .py 
  :13310 	    :F:\python\translate  .py 
  :632051 	    :F:\python\          .py 
  :3338255 	    :F:\python\     .py 
  :922580 	    :F:\python\        .py 
  :30578 	    :F:\python\dome\models.py 
  :681765 	    :F:\python\dome\session.py 
  :1012326 	    :F:\python\dome\spider.py 
  :10286 	    :F:\python\Python    \1. Python     (      ).py 
  :672212 	    :F:\python\Python    \10. threading-Python Queue        .py 
  :1083204 	    :F:\python\Python    \11. threading-Python Event      .py 
  :1243517 	    :F:\python\Python    \12. threading-Python          (    ).py 
  :33820 	    :F:\python\Python    \13. threading-Python threading Local().py 
  :29548 	    :F:\python\Python    \14. threading-Python Timer   :           .py 
  :461465 	    :F:\python\Python    \15. Python schedule        .py 
  :12265 	    :F:\python\Python    \16. Python os.fork().py 
  :832070 	    :F:\python\Python    \17. multiprocessing-Python Process    (2.py 
  :481016 	    :F:\python\Python    \18. multiprocessing-Python       3   .py 
  :17439 	    :F:\python\Python    \19.               .py 
  :611884 	    :F:\python\Python    \2. Python    (2.py 
  :602029 	    :F:\python\Python    \20. multiprocessing-Python         .py 
  :661993 	    :F:\python\Python    \21. multiprocessing-Python      2     (Queue Pipe).py 
  :832226 	    :F:\python\Python    \22. Python Futures      .py 
  :671872 	    :F:\python\Python    \23. Python Asyncio      .py 
  :36590 	    :F:\python\Python    \24. Python GIL        (    ).py 
  :1472965 	    :F:\python\Python    \25.     Python      (    ).py 
  :621645 	    :F:\python\Python    \3. Python       (  、  、  、     ).py 
  :22563 	    :F:\python\Python    \4. threading-Python Thread join()    .py 
  :22614 	    :F:\python\Python    \5. threading-Python       (  2.py 
  :8123 	    :F:\python\Python    \6. threading-Python sleep().py 
  :1013098 	    :F:\python\Python    \7. threading-Python   (Lock):         .py 
  :822193 	    :F:\python\Python    \8. threading-     ,      (4.py 
  :972904 	    :F:\python\Python    \9. threading-Python condition      (   ).py 
  :581 	    :F:\python\Python    \  .txt 
   :336694452