Multithreaded Read files:
# _*_coding:utf-8_*_import time, threading, Configparser" "Reader class, inheriting threading. The Thread@__init__ method initializes the @run method to realize the operation of the read file ." "classReader (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') " "the If block is the main judge of the block after the first place of the file block is not the beginning, is the beginning of the word, do not handle otherwise, the first position of the file block is positioned to the beginning of the next line" " ifSelf.start_pos! =0: Fd.seek (Self.start_pos-1) ifFd.read (1) !='\ n': Line=fd.readline () Self.start_pos=Fd.tell () Fd.seek (Self.start_pos)" "to process the file block" " while(Self.start_pos <=self.end_pos): Line=Fd.readline ()" " Dosomthing" "Self.start_pos =Fd.tell ()" "block the file, the number of file blocks and the number of threads is consistent" "classPartition (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 forIinchRange (self.block_num):ifi = = self.block_num-1: End_pos= file_size-1Pos_list.append ((Start_pos, end_pos)) BreakEnd_pos= start_pos+block_size-1 ifEnd_pos >=File_size:end_pos= file_size-1 ifStart_pos >=file_size: BreakPos_list.append ((Start_pos, End_pos)) Start_pos= end_pos+1fd.close ()returnpos_listif__name__ = ='__main__': " "reading configuration Files" "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 () #生成线程 forIinchRange (Thread_num): T.append (Reader (file_name,*Pos[i])) #开启线程 forIinchRange (Thread_num): T[i].start () forIinchRange (Thread_num): T[i].join () #结束时间 end_time=Time.clock () print"Cost time is %f"% (End_time-start_time)
Linux Learning (35): multithreaded read files