繁体   English   中英

Python线程,测量进度百分比

[英]Python threading, measuring progress percentage

这是一种“最佳做法”问题。 我正在做我的第一个多线程代码,我想知道自己是否在正确地衡量进度。 这是我的代码,它一次执行16个文件复制线程,而我正在尝试创建一个进度条。 这段代码有效,但是我想知道这是否是“正确”的方法(例如,如果多个线程一次写入“ copyCount”,该怎么办?):

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()

对于其他感兴趣的人,我肯定是做错了。 我找到了这篇奇妙的文章,它用非常简单的术语解释了如何使用锁来处理多个线程: 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM