欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python多線程分塊讀取文件

 更新時(shí)間:2019年08月29日 09:57:31   作者:美如畫是我  
這篇文章主要為大家詳細(xì)介紹了python多線程分塊讀取文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python多線程分塊讀取文件的具體代碼,供大家參考,具體內(nèi)容如下

# _*_coding:utf-8_*_
import time, threading, ConfigParser
 
'''
Reader類,繼承threading.Thread
@__init__方法初始化
@run方法實(shí)現(xiàn)了讀文件的操作
'''
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)
    '''
    對(duì)該文件塊進(jìn)行處理
    '''
    while (self.start_pos <= self.end_pos):
      line = fd.readline()
      '''
      do somthing
      '''
      self.start_pos = fd.tell()
 
'''
對(duì)文件進(jìn)行分塊,文件塊的數(shù)量和線程數(shù)量一致
'''
class Partition(object):
  def __init__(self, file_name, 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')
  #線程數(shù)量
  thread_num = int(config.get('info', 'threadNum'))
  #起始時(shí)間
  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()
  #結(jié)束時(shí)間
  end_time = time.clock()
  print "Cost time is %f" % (end_time - start_time)

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)設(shè)置windows桌面壁紙代碼分享

    Python實(shí)現(xiàn)設(shè)置windows桌面壁紙代碼分享

    這篇文章主要介紹了Python實(shí)現(xiàn)設(shè)置windows桌面壁紙,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-03-03
  • 詳解pandas.DataFrame.plot() 畫圖函數(shù)

    詳解pandas.DataFrame.plot() 畫圖函數(shù)

    這篇文章主要介紹了詳解pandas.DataFrame.plot()畫圖函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 基于python 處理中文路徑的終極解決方法

    基于python 處理中文路徑的終極解決方法

    下面小編就為大家分享一篇基于python 處理中文路徑的終極解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python?pomegranate庫實(shí)現(xiàn)基于貝葉斯網(wǎng)絡(luò)拼寫檢查器

    Python?pomegranate庫實(shí)現(xiàn)基于貝葉斯網(wǎng)絡(luò)拼寫檢查器

    這篇文章主要為大家介紹了Python?pomegranate庫實(shí)現(xiàn)基于貝葉斯網(wǎng)絡(luò)拼寫檢查器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2023-04-04
  • 深入了解Python中pop和remove的使用方法

    深入了解Python中pop和remove的使用方法

    這篇文章主要介紹了深入了解Python中pop和remove的使用方法,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 對(duì)Python 窗體(tkinter)文本編輯器(Text)詳解

    對(duì)Python 窗體(tkinter)文本編輯器(Text)詳解

    今天小編就為大家分享一篇對(duì)Python 窗體(tkinter)文本編輯器(Text)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python Crypto模塊的安裝與使用方法

    python Crypto模塊的安裝與使用方法

    本篇文章主要介紹了python Crypto模塊的安裝與使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Python編程基礎(chǔ)之運(yùn)算符重載詳解

    Python編程基礎(chǔ)之運(yùn)算符重載詳解

    這篇文章主要為大家詳細(xì)介紹了Python運(yùn)算符重載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • python KNN算法實(shí)現(xiàn)鳶尾花數(shù)據(jù)集分類

    python KNN算法實(shí)現(xiàn)鳶尾花數(shù)據(jù)集分類

    這篇文章主要介紹了python KNN算法實(shí)現(xiàn)鳶尾花數(shù)據(jù)集分類,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python PyQt5模塊實(shí)現(xiàn)一個(gè)瀏覽器的示例代碼

    Python PyQt5模塊實(shí)現(xiàn)一個(gè)瀏覽器的示例代碼

    在項(xiàng)目開發(fā)中,有的應(yīng)用程序可以運(yùn)行在web瀏覽器,本文主要介紹了Python PyQt5模塊實(shí)現(xiàn)一個(gè)瀏覽器的示例代碼,分享給大家,感興趣的可以了解一下
    2021-07-07

最新評(píng)論