欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)模擬分割大文件及多線程處理的方法

 更新時間:2017年10月10日 10:56:17   作者:Jredreamer  
這篇文章主要介紹了Python實(shí)現(xiàn)模擬分割大文件及多線程處理的方法,涉及Python文件讀取、分割及多線程相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)模擬分割大文件及多線程處理的方法。分享給大家供大家參考,具體如下:

#!/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):
  """具體的任務(wù)類"""
  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()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論