Python进度栏-是否在这里回答问题? [英] Python Progress Bar - Is Threading the Answer Here?

查看:88
本文介绍了Python进度栏-是否在这里回答问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对Python中的进度条进行了一些研究,许多解决方案似乎都是基于将工作分成已知的离散块的.即,迭代一次已知的次数,并在每次迭代结束时取得进度的一个百分点时,用stdout更新进度条.

I've done some research on progress bars in Python, and a lot of the solutions seem to be based on work being divided into known, discrete chunks. I.e., iterating a known number of times and updating the progress bar with stdout every time a percentage point of the progress toward the end of the iterations is made.

我的问题是离散问题.它涉及遍历包含数百个子目录的用户目录,收集MP3信息,并将其输入数据库.我可能可以在迭代之前计算目录中MP3文件的数量,并将其用作离散块的准则,但是许多mp3可能已经存在于数据库中,其中某些文件的读取时间比其他文件要长,因此会出现错误.发生并且必须在某些情况下进行处理等等.此外,我想知道如何使用非离散块将其实现,以供将来参考.如果您有兴趣,这是我的directory-walk/database-update的代码:

My problem is a little less discrete. It involves walking a user directory that contains hundreds of sub-directories, gathering MP3 information, and entering it into a database. I could probably count the number of MP3 files in the directory before iteration and use that as a guideline for discrete chunks, but many of the mp3s may already be in the database, some of the files will take longer to read than others, errors will occur and have to be handled in some cases, etc. Besides, I'd like to know how to pull this off with non-discrete chunks for future reference. Here is the code for my directory-walk/database-update, if you're interested:

import mutagen
import sys
import os
import sqlite3 as lite
for root, dirs, files in os.walk(startDir):

    for file in files:
        if isMP3(file):
            fullPath = os.path.join(root, file)

            # Check if path already exists in DB, skip iteration if so
            if unicode(fullPath, errors="replace") in pathDict:
                continue

            try:
                audio = MP3(fullPath)
            except mutagen.mp3.HeaderNotFoundError: # Invalid file/ID3 info
                #TODO: log for user to look up what files were not visitable
                continue
            # Do database operations and error handling therein. 

线程是处理此类问题的最佳方法吗?如果是这样,关于线程如何实现此目标,是否有任何好的示例?我不想为此使用一个模块,因为(a)似乎我应该知道该怎么做,以及(b)我正在针对依赖轻度的情况进行开发.

Is threading the best way to approach something like this? And if so, are there any good examples on how threading achieves this? I don't want a module for this because (a) it seems like something I should know how to do and (b) I'm developing for a dependency-lite situation.

推荐答案

如果您不知道面前有多少步骤,那么如何获得进展?那是第一件事.开始工作之前,您必须计算所有这些信息.

If you don't know how many steps are in front of you, then how can you get a progress? That's the first thing. You have to count all of them before starting the job.

现在,即使任务在完成所需的时间方面有所不同,您也不必为此担心.想想游戏.有时,当您看到进度条时,它们似乎停在一个点上,然后跳得很快.这正是幕后发生的事情:有些任务要比其他任务花费更长的时间.但这没什么大不了的(除非任务真的很长,例如分钟?).

Now even if tasks differ in terms of needed time to finish you should not worry about that. Think about games. Sometimes when you see progress bars they seem to stop in one point and then jump very fast. This is exactly what's happening under the hood: some tasks take longer then others. But it's not a big deal ( unless the task is really long, like minutes maybe? ).

您当然可以使用线程.实际上,使用Queue和ThreadPool可能很简单.例如运行20个线程并构建一个作业队列.这样,您的进度将是Queue中的项目数,并且以Queue的初始长度为限制.这似乎是一个不错的设计.

Of course you can use threads. It might be quite simple actually with Queue and ThreadPool. Run for example 20 threads and build a Queue of jobs. Your progress would then be number of items in Queue with initial length of Queue as a limit. This seems like a good design.

这篇关于Python进度栏-是否在这里回答问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆