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('\n'))
        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]} \n")
            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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值