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()
當_writing_到'copyCount'你應該用鎖以確保線程安全,即'鎖定:copyCount + = 1'。這消除了錯誤計數的可能性。您也可以選擇通過參數而不是全局變量傳遞'CopyWorked'文件列表。您也可以選擇縮短'CopyWorker'的長度,並在主線程的函數中進行進度計算。 – 101
嘿101,謝謝!我會更深入地瞭解關於鎖定的文檔,聽起來像我需要知道的。你會如何將進度計算放在主線程中?我不知道我會怎麼做。 – Spencer
您可以簡單地執行類似'while copyCount
101