简体   繁体   English

Python线程,测量进度百分比

[英]Python threading, measuring progress percentage

This is a sort of "best practices" question. 这是一种“最佳做法”问题。 I'm doing my first multi-threaded code and I'm wondering if I am measuring progress properly. 我正在做我的第一个多线程代码,我想知道自己是否在正确地衡量进度。 Here's my code which does 16 file-copy threads at once, and I'm trying to create a progress bar. 这是我的代码,它一次执行16个文件复制线程,而我正在尝试创建一个进度条。 This code works but I want to know if this is the "right" way of doing it (for example what if multiple threads are writing to "copyCount" at once?): 这段代码有效,但是我想知道这是否是“正确”的方法(例如,如果多个线程一次写入“ 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()

For anyone else interested yes I was definitely doing this wrong. 对于其他感兴趣的人,我肯定是做错了。 I found this fantastic article which explains in pretty simple terms how to go about wrangling multiple threads using locks: http://effbot.org/zone/thread-synchronization.htm 我找到了这篇奇妙的文章,它用非常简单的术语解释了如何使用锁来处理多个线程: http : //effbot.org/zone/thread-synchronization.htm

And for an updated code sample: 对于更新的代码示例:

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