2017-08-05 147 views
0

這是一種「最佳實踐」問題。我正在做我的第一個多線程代碼,我想知道我是否正確測量進度。這是我的代碼,它一次執行16個文件複製線程,並且我正在嘗試創建一個進度條。此代碼的工作,但我想知道這是做的「正確」的方式(例如,如果什麼多個線程寫入「copyCount」一次?):Python線程,測量進度百分比

import Queue, threading, os 
import shutil 

fileQueue = Queue.Queue() 
destPath = 'destination/folder/here' 

class ThreadedCopy: 
    totalFiles = 0 
    copyCount = 0 

    def __init__(self): 
     with open("filelist.txt", "r") as txt: 
      fileList = txt.read().splitlines() 

     if not os.path.exists(destPath): 
      os.mkdir(destPath) 

     self.totalFiles = len(fileList) 

     print str(self.totalFiles) + " files to copy." 
     self.threadWorkerCopy(fileList) 

    def CopyWorker(self): 
     while True: 
      fileName = fileQueue.get() 
      shutil.copy(fileName, destPath) 
      fileQueue.task_done() 
      self.copyCount += 1 
      percent = (self.copyCount*100)/self.totalFiles 
      print str(percent) + " percent copied." 

    def threadWorkerCopy(self, fileNameList): 
     for i in range(16): 
      t = threading.Thread(target=self.CopyWorker) 
      t.daemon = True 
      t.start() 
     for fileName in fileNameList: 
      fileQueue.put(fileName) 
     fileQueue.join() 

ThreadedCopy() 
+0

當_writing_到'copyCount'你應該用鎖以確保線程安全,即'鎖定:copyCount + = 1'。這消除了錯誤計數的可能性。您也可以選擇通過參數而不是全局變量傳遞'CopyWorked'文件列表。您也可以選擇縮短'CopyWorker'的長度,並在主線程的函數中進行進度計算。 – 101

+0

嘿101,謝謝!我會更深入地瞭解關於鎖定的文檔,聽起來像我需要知道的。你會如何將進度計算放在主線程中?我不知道我會怎麼做。 – Spencer

+0

您可以簡單地執行類似'while copyCount 101

回答

0

對於任何人感興趣的是我肯定是做錯了。我發現這個神奇的物品,其解釋非常簡單來說如何去使用鎖扯皮多線程:http://effbot.org/zone/thread-synchronization.htm

而對於更新的代碼示例:

import Queue, threading, os 
import shutil 

fileQueue = Queue.Queue() 
destPath = 'destination/folder/here' 

class ThreadedCopy: 
    totalFiles = 0 
    copyCount = 0 
    lock = threading.Lock() 

    def __init__(self): 
     with open("filelist.txt", "r") as txt: 
      fileList = txt.read().splitlines() 

     if not os.path.exists(destPath): 
      os.mkdir(destPath) 

     self.totalFiles = len(fileList) 

     print str(self.totalFiles) + " files to copy." 
     self.threadWorkerCopy(fileList) 

    def CopyWorker(self): 
     while True: 
      fileName = fileQueue.get() 
      shutil.copy(fileName, destPath) 
      fileQueue.task_done() 
      with self.lock: 
       self.copyCount += 1 
       percent = (self.copyCount*100)/self.totalFiles 
       print str(percent) + " percent copied." 

    def threadWorkerCopy(self, fileNameList): 
     for i in range(16): 
      t = threading.Thread(target=self.CopyWorker) 
      t.daemon = True 
      t.start() 
     for fileName in fileNameList: 
      fileQueue.put(fileName) 
     fileQueue.join() 

ThreadedCopy()