随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

多进程、多线程处理文件对比

分别通过多进程、多线程方式处理文件,将结果保存到一个list中:

1.多进程:

复制代码
import multiprocessing,cjson,os,collections
from multiprocessing import Process,freeze_support,Manager,Pool,Queue

def handlefile(lock,rst,fp):
    lst_tmp=[]
    #print type(rst)
    with open(fp,'rb') as fo:
        for line in fo:
            line = cjson.decode(line)
            lst_tmp.append(line['s-ip'])
    #print collections.Counter(lst_tmp)
    lock.acquire()
    rst.extend(lst_tmp)
    lock.release()


if __name__ == '__main__':
    lock = Manager().Lock()
    rst = Manager().list()

    starttime = datetime.datetime.now()
    f1 = 'e:\\logtest\\iis__20160519105745.json'
    f2 = 'e:\\logtest\\iis__20160519105816.json'
    f3 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134616.json'
    f4 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134955.json'
    f5 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134616.json'
    f6 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134955.json'
    files = [f1,f2,f3,f4,f5,f6]
    p=Pool()
    for file in files:
        p.apply_async(handlefile,args=(lock,rst,file))
    p.close()
    p.join()

    print collections.Counter(rst)

    print (datetime.datetime.now() - starttime).total_seconds() #耗时16.631s
复制代码

 

2.多线程:

复制代码
import threading
global rst
rst = []
def query(mutex,fp):
    lst_tmp=[]
    #print type(rst)
    with open(fp,'rb') as fo:
        for line in fo:
            line = cjson.decode(line)
            lst_tmp.append(line['s-ip'])
    #print collections.Counter(lst_tmp)
    mutex.acquire()                   #可以改写为with mutex(),替换掉acquire + release()
    rst.extend(lst_tmp)
    mutex.release()


if __name__ == '__main__':
    threads=[]
    mutex=threading.Lock()
    starttime = datetime.datetime.now()
    f1 = 'e:\\logtest\\iis__20160519105745.json'
    f2 = 'e:\\logtest\\iis__20160519105816.json'
    f3 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134616.json'
    f4 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134955.json'
    f5 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134616.json'
    f6 = 'e:\\logtest\\iis_IDC-ExFE01_20160524134955.json'
    files = [f1,f2,f3,f4,f5,f6]

    for filepath in files:
        t = threading.Thread(target=query,args=(mutex,filepath))
        t.setDaemon(True)
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

    print collections.Counter(rst)

    print (datetime.datetime.now() - starttime).total_seconds() #耗时4.425s
复制代码

 

结论:多进程和多线程在分别处理每个文件,将结果写入各自tmp list中,多线程耗时2.468s,多线程耗时4.24s,多进程优于多线程(进程数量未控制,默认CPU核心数量)。

        但当多线程各结果写入到共享变量list()时,多线程严重耗时较久,多线程共计耗时4.425s,多进程耗时16.631s。多进程中的共享变量效率低下。

 

posted on   momingliu11  阅读(954)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-05-27 读取xml格式文件
2013-05-27 Network Monitor
2013-05-27 循环while、foreach-object
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示