Python實(shí)現(xià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)文章
Pytorch中關(guān)于RNN輸入和輸出的形狀總結(jié)
這篇文章主要介紹了Pytorch中關(guān)于RNN輸入和輸出的形狀總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式
這篇文章主要介紹了TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python執(zhí)行時間的計(jì)算方法小結(jié)
這篇文章主要介紹了Python執(zhí)行時間的計(jì)算方法小結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03pyinstaller將含有多個py文件的python程序做成exe
這篇文章主要介紹了pyinstaller將含有多個py文件的python程序做成exe,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python自動化測試selenium核心技術(shù)等待條件教程
這篇文章主要為大家介紹了python自動化測試selenium核心技術(shù)等待條件教程的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11在Python中利用Into包整潔地進(jìn)行數(shù)據(jù)遷移的教程
這篇文章主要介紹了在Python中如何利用Into包整潔地進(jìn)行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下2015-03-03