Python多進(jìn)程分塊讀取超大文件的方法
本文實(shí)例講述了Python多進(jìn)程分塊讀取超大文件的方法。分享給大家供大家參考,具體如下:
讀取超大的文本文件,使用多進(jìn)程分塊讀取,將每一塊單獨(dú)輸出成文件
# -*- coding: GBK -*-
import urlparse
import datetime
import os
from multiprocessing import Process,Queue,Array,RLock
"""
多進(jìn)程分塊讀取文件
"""
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
"""
進(jìn)程處理
Args:
pid:進(jìn)程編號
array:進(jìn)程間共享隊(duì)列,用于標(biāo)記各進(jìn)程所讀的文件塊結(jié)束位置
file:所讀文件名稱
各個進(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截斷的情況,先讀一行不處理,從下一行開始正式處理
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文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python多進(jìn)程并發(fā)(multiprocessing)用法實(shí)例詳解
- Python中使用多進(jìn)程來實(shí)現(xiàn)并行處理的方法小結(jié)
- 淺析Python中的多進(jìn)程與多線程的使用
- Python多進(jìn)程通信Queue、Pipe、Value、Array實(shí)例
- Python多進(jìn)程庫multiprocessing中進(jìn)程池Pool類的使用詳解
- Python控制多進(jìn)程與多線程并發(fā)數(shù)總結(jié)
- 探究Python多進(jìn)程編程下線程之間變量的共享問題
- Python多進(jìn)程同步Lock、Semaphore、Event實(shí)例
- Python多進(jìn)程multiprocessing.Pool類詳解
- Python多進(jìn)程與多線程的使用場景詳解
相關(guān)文章
python如何實(shí)現(xiàn)全角半角的相互轉(zhuǎn)換
這篇文章主要介紹了python如何實(shí)現(xiàn)全角半角的相互轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
OpenCV實(shí)戰(zhàn)之OpenCV中的顏色空間
這篇文章主要介紹了OpenCV實(shí)戰(zhàn)之OpenCV中的顏色空間,解計算機(jī)視覺中常用的色彩空間,并將其用于基于顏色分割。我們還將用C?++和Python共享演示代碼,下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-04-04
python 爬蟲百度地圖的信息界面的實(shí)現(xiàn)方法
這篇文章主要介紹了python 爬蟲百度地圖的界面的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

