Python多进程分块读取超大文件的方法

yipeiwu_com7年前Python基础

本文实例讲述了Python多进程分块读取超大文件的方法。分享给大家供大家参考,具体如下:

读取超大的文本文件,使用多进程分块读取,将每一块单独输出成文件

# -*- coding: GBK -*-
import urlparse
import datetime
import os
from multiprocessing import Process,Queue,Array,RLock
"""
多进程分块读取文件
"""
WORKERS = 4
BLOCKSIZE = 100000000
FILE_SIZE = 0
def getFilesize(file):
  """
    获取要读取文件的大小
  """
  global FILE_SIZE
  fstream = open(file,'r')
  fstream.seek(0,os.SEEK_END)
  FILE_SIZE = fstream.tell()
  fstream.close()
def process_found(pid,array,file,rlock):
  global FILE_SIZE
  global JOB
  global PREFIX
  """
    进程处理
    Args:
      pid:进程编号
      array:进程间共享队列,用于标记各进程所读的文件块结束位置
      file:所读文件名称
    各个进程先从array中获取当前最大的值为起始位置startpossition
    结束的位置endpossition (startpossition+BLOCKSIZE) if (startpossition+BLOCKSIZE)<FILE_SIZE else FILE_SIZE
    if startpossition==FILE_SIZE则进程结束
    if startpossition==0则从0开始读取
    if startpossition!=0为防止行被block截断的情况,先读一行不处理,从下一行开始正式处理
    if 当前位置 <=endpossition 就readline
    否则越过边界,就从新查找array中的最大值
  """
  fstream = open(file,'r')
  while True:
    rlock.acquire()
    print 'pid%s'%pid,','.join([str(v) for v in array])
    startpossition = max(array)      
    endpossition = array[pid] = (startpossition+BLOCKSIZE) if (startpossition+BLOCKSIZE)<FILE_SIZE else FILE_SIZE
    rlock.release()
    if startpossition == FILE_SIZE:#end of the file
      print 'pid%s end'%(pid)
      break
    elif startpossition !=0:
      fstream.seek(startpossition)
      fstream.readline()
    pos = ss = fstream.tell()
    ostream = open('/data/download/tmp_pid'+str(pid)+'_jobs'+str(endpossition),'w')
    while pos<endpossition:
      #处理line
      line = fstream.readline()
      ostream.write(line)
      pos = fstream.tell()
    print 'pid:%s,startposition:%s,endposition:%s,pos:%s'%(pid,ss,pos,pos)
    ostream.flush()
    ostream.close()
    ee = fstream.tell()
  fstream.close()
def main():
  global FILE_SIZE
  print datetime.datetime.now().strftime("%Y/%d/%m %H:%M:%S") 
  file = "/data/pds/download/scmcc_log/tmp_format_2011004.log"
  getFilesize(file)
  print FILE_SIZE
  rlock = RLock()
  array = Array('l',WORKERS,lock=rlock)
  threads=[]
  for i in range(WORKERS):
    p=Process(target=process_found, args=[i,array,file,rlock])
    threads.append(p)
  for i in range(WORKERS):
    threads[i].start()
  for i in range(WORKERS):
    threads[i].join()
  print datetime.datetime.now().strftime("%Y/%d/%m %H:%M:%S") 
if __name__ == '__main__':
  main()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

相关文章

python实现教务管理系统

这是一个使用Python实现基于dos下面向数据库的教务管理系统,实现了管理员、教职工、学生三种不同身份的操作,可以实现的功能有:学生、教职工信息管理、不同权限的信息发布、管理各种信息等...

python通过nmap扫描在线设备并尝试AAA登录(实例代码)

如果管理网络设备很多,不可能靠人力每天去登录设备去查看是否在线。所以,可以利用python脚本通过每天扫描网络中的在线设备。可以部署在服务器上做成定时任务,每天发送AAA巡检报告。 下面...

Python中的rfind()方法使用详解

 rfind()方法返回所在子str 被找到的最后一个索引,或者-1,如果没有这样的索引不存在,可选择限制搜索字符串string[beg:end]. 语法 以下是rfind()...

浅析python3字符串格式化format()函数的简单用法

 format()函数 """ 测试 format()函数 """ def testFormat(): # format()函数中有几个元素,前面格式化的字符串中就要有...

ActiveMQ:使用Python访问ActiveMQ的方法

ActiveMQ:使用Python访问ActiveMQ的方法

Windows 10家庭中文版,Python 3.6.4,stomp.py 4.1.21 ActiveMQ支持Python访问,提供了基于STOMP协议(端口为61613)的库。 Act...