Python multiprocessing Pool map()インスタンス
58299 ワード
理由:あるディレクトリの下のファイルの文字数と行数を並列に処理し、res.txtファイルに格納する
出力:
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ファイル:
:60 :1542 :F:\python\1.py
:834 :26082 :F:\python\1.txt
:47 :1184 :F:\python\Barrier .py
:60 :1368 :F:\python\condition .py
:52 :1225 :F:\python\debug .py
:43 :2773 :F:\python\res.txt
:34 :785 :F:\python\semaphore .py
:1 :14 :F:\python\test.py
:39 :992 :F:\python\threading .py
:13 :310 :F:\python\translate .py
:63 :2051 :F:\python\ .py
:333 :8255 :F:\python\ .py
:92 :2580 :F:\python\ .py
:30 :578 :F:\python\dome\models.py
:68 :1765 :F:\python\dome\session.py
:101 :2326 :F:\python\dome\spider.py
:10 :286 :F:\python\Python \1. Python ( ).py
:67 :2212 :F:\python\Python \10. threading-Python Queue .py
:108 :3204 :F:\python\Python \11. threading-Python Event .py
:124 :3517 :F:\python\Python \12. threading-Python ( ).py
:33 :820 :F:\python\Python \13. threading-Python threading Local() : .py
:29 :548 :F:\python\Python \14. threading-Python Timer : .py
:46 :1465 :F:\python\Python \15. Python schedule .py
:12 :265 :F:\python\Python \16. Python os.fork() : .py
:83 :2070 :F:\python\Python \17. multiprocessing-Python Process (2 ) .py
:48 :1016 :F:\python\Python \18. multiprocessing-Python 3 .py
:17 :439 :F:\python\Python \19. .py
:61 :1884 :F:\python\Python \2. Python (2 ) .py
:60 :2029 :F:\python\Python \20. multiprocessing-Python .py
:66 :1993 :F:\python\Python \21. multiprocessing-Python 2 (Queue Pipe).py
:83 :2226 :F:\python\Python \22. Python Futures .py
:67 :1872 :F:\python\Python \23. Python Asyncio .py
:36 :590 :F:\python\Python \24. Python GIL ( ).py
:147 :2965 :F:\python\Python \25. Python ( ).py
:62 :1645 :F:\python\Python \3. Python ( 、 、 、 ).py
:22 :563 :F:\python\Python \4. threading-Python Thread join() .py
:22 :614 :F:\python\Python \5. threading-Python ( 2 ).py
:8 :123 :F:\python\Python \6. threading-Python sleep() : .py
:101 :3098 :F:\python\Python \7. threading-Python (Lock): .py
:82 :2193 :F:\python\Python \8. threading- , (4 ).py
:97 :2904 :F:\python\Python \9. threading-Python condition ( ).py
:5 :81 :F:\python\Python \ .txt
:3366 :94452