Python analog split large file and multithreading implementation method

Source: Internet
Author: User
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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.