2017-08-05 146 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()