Home > Backend Development > Python Tutorial > Python simulation method of splitting large files and implementing multi-thread processing

Python simulation method of splitting large files and implementing multi-thread processing

黄舟
Release: 2018-05-26 10:10:38
Original
3182 people have browsed it

This article mainly introduces the method of Python to implement simulated splitting of large files and multi-threaded processing, involving Python file reading, splitting and multi-threading related operation skills. Friends in need can refer to the examples of this article

Describes how Python implements simulation of splitting large files and multi-threaded processing. Share it with everyone for your reference, the details are as follows:

#!/usr/bin/env python
#--*-- coding:utf-8 --*--
from random import randint
from time import ctime
from time import sleep
import queue
import threading
class MyTask(object):
  """具体的任务类"""
  def __init__(self, name):
    self.name = name
    self._work_time = 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):
  """多线程的类"""
  def __init__(self, my_queue):
    self.my_queue = my_queue
    super(MyThread, self).__init__()
  def run(self):
    while True:
      if self.my_queue.qsize() > 0:
        self.my_queue.get().work()
      else:
        break
def print_split_line(num=30):
  print("*" * num)
if __name__ == "__main__":
  print_split_line()
  import my_read_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()
Copy after login

The above is the detailed content of Python simulation method of splitting large files and implementing multi-thread processing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template