1. 程式人生 > 程式設計 >python多執行緒分塊讀取檔案

python多執行緒分塊讀取檔案

本文例項為大家分享了python多執行緒分塊讀取檔案的具體程式碼,供大家參考,具體內容如下

# _*_coding:utf-8_*_
import time,threading,ConfigParser
 
'''
Reader類,繼承threading.Thread
@__init__方法初始化
@run方法實現了讀檔案的操作
'''
class Reader(threading.Thread):
  def __init__(self,file_name,start_pos,end_pos):
    super(Reader,self).__init__()
    self.file_name = file_name
    self.start_pos = start_pos
    self.end_pos = end_pos
 
  def run(self):
    fd = open(self.file_name,'r')
    '''
    該if塊主要判斷分塊後的檔案塊的首位置是不是行首,
    是行首的話,不做處理
    否則,將檔案塊的首位置定位到下一行的行首
    '''
    if self.start_pos != 0:
      fd.seek(self.start_pos-1)
      if fd.read(1) != '\n':
        line = fd.readline()
        self.start_pos = fd.tell()
    fd.seek(self.start_pos)
    '''
    對該檔案塊進行處理
    '''
    while (self.start_pos <= self.end_pos):
      line = fd.readline()
      '''
      do somthing
      '''
      self.start_pos = fd.tell()
 
'''
對檔案進行分塊,檔案塊的數量和執行緒數量一致
'''
class Partition(object):
  def __init__(self,thread_num):
    self.file_name = file_name
    self.block_num = thread_num
 
  def part(self):
    fd = open(self.file_name,'r')
    fd.seek(0,2)
    pos_list = []
    file_size = fd.tell()
    block_size = file_size/self.block_num
    start_pos = 0
    for i in range(self.block_num):
      if i == self.block_num-1:
        end_pos = file_size-1
        pos_list.append((start_pos,end_pos))
        break
      end_pos = start_pos+block_size-1
      if end_pos >= file_size:
        end_pos = file_size-1
      if start_pos >= file_size:
        break
      pos_list.append((start_pos,end_pos))
      start_pos = end_pos+1
    fd.close()
    return pos_list
 
if __name__ == '__main__':
  '''
  讀取配置檔案
  '''
  config = ConfigParser.ConfigParser()
  config.readfp(open('conf.ini'))
  #檔名
  file_name = config.get('info','fileName')
  #執行緒數量
  thread_num = int(config.get('info','threadNum'))
  #起始時間
  start_time = time.clock()
  p = Partition(file_name,thread_num)
  t = []
  pos = p.part()
  #生成執行緒
  for i in range(thread_num):
    t.append(Reader(file_name,*pos[i]))
  #開啟執行緒
  for i in range(thread_num):
    t[i].start()
  for i in range(thread_num):
    t[i].join()
  #結束時間
  end_time = time.clock()
  print "Cost time is %f" % (end_time - start_time)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。