国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多進(jìn)程分塊讀取超大文件的方法

2019-11-25 16:50:46
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python多進(jìn)程分塊讀取超大文件的方法。分享給大家供大家參考,具體如下:

讀取超大的文本文件,使用多進(jìn)程分塊讀取,將每一塊單獨(dú)輸出成文件

# -*- coding: GBK -*-import urlparseimport datetimeimport osfrom multiprocessing import Process,Queue,Array,RLock"""多進(jìn)程分塊讀取文件"""WORKERS = 4BLOCKSIZE = 100000000FILE_SIZE = 0def 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  """    進(jìn)程處理    Args:      pid:進(jìn)程編號(hào)      array:進(jìn)程間共享隊(duì)列,用于標(biāo)記各進(jìn)程所讀的文件塊結(jié)束位置      file:所讀文件名稱    各個(gè)進(jìn)程先從array中獲取當(dāng)前最大的值為起始位置startpossition    結(jié)束的位置endpossition (startpossition+BLOCKSIZE) if (startpossition+BLOCKSIZE)<FILE_SIZE else FILE_SIZE    if startpossition==FILE_SIZE則進(jìn)程結(jié)束    if startpossition==0則從0開始讀取    if startpossition!=0為防止行被block截?cái)嗟那闆r,先讀一行不處理,從下一行開始正式處理    if 當(dāng)前位置 <=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()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灌云县| 西乌珠穆沁旗| 高尔夫| 宕昌县| 宾川县| 汝阳县| 霍城县| 扶沟县| 浪卡子县| 宿松县| 肇庆市| 普洱| 辰溪县| 肥城市| 普陀区| 新绛县| 仪征市| 什邡市| 台州市| 洛南县| 新邵县| 麻城市| 潜江市| 中阳县| 保康县| 慈溪市| 金湖县| 南丹县| 民和| 奈曼旗| 永定县| 黔西县| 吉安市| 扶风县| 永嘉县| 绥化市| 岳阳县| 虞城县| 郓城县| 东至县| 浦江县|