Python類比分割大檔案以及多執行緒的實現方法

來源:互聯網
上載者:User
這篇文章主要介紹了Python實現類比分割大檔案及多執行緒的方法,涉及Python檔案讀取、分割及多線程相關操作技巧,需要的朋友可以參考下

本文執行個體講述了Python實現類比分割大檔案及多執行緒的方法。分享給大家供大家參考,具體如下:

#!/usr/bin/env python#--*-- coding:utf-8 --*--from random import randintfrom time import ctimefrom time import sleepimport queueimport threadingclass 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:        breakdef 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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.