This article mainly introduces Python to realize the simulation of large files and multi-threaded processing methods, involving Python file reading, segmentation and multithreading related operation skills, the need for friends can refer to the next
In this paper, we describe the method of Python to realize large file segmentation and multithreading. Share to everyone for your reference, as follows:
#!/usr/bin/env python#--*--coding:utf-8--*--from Random import randintfrom time import ctimefrom time import Sleepimport Queueimport threadingclass MyTask (object): "" "Specific Task Class" "" Def __init__ (self, name): Self.name = name Self._work_tim E = Randint (1, 5) def work (self): print ("Task%s is start:%s, sleep time=%d"% (Self.name, CTime (), Self._work_time ) sleep (self._work_time) print ("Task%s is end:%s"% (Self.name, CTime ())) class MyThread (threading. Thread): "" "Multithreaded Class" "" Def __init__ (Self, my_queue): Self.my_queue = My_queue Super (MyThread, self). __init__ () def Run (self): when True:if self.my_queue.qsize () > 0:self.my_queue.get (). Work () ELSE:BREAKD EF Print_split_line (num=30): Print ("*" * num) if __name__ = = "__main__": Print_split_line () Import my_read_file # split file SF = My_read_file. Splitfiles (R "F:\multiple_thread_read_file.txt", line_count=300) File_num = Sf.split_file () queue_length = File_num My_ Queue = queue. LifoquEue (queue_length) threads = [] for I in range (queue_length): file_name = Sf.get_part_file_name (i) MT = mytask (file _name) my_queue.put_nowait (MT) for I in Range (queue_length): MTD = MyThread (my_queue) threads.append (MTD) for I In range (queue_length): Threads[i].start () for I in Range (queue_length): Threads[i].join () Print_split_line ()