Python多线程进度条progressbar没能达到预期,pythonprogressbar,Python线程池代码#


Python线程池代码

# !/usr/bin/env python# -*- coding:utf-8 -*-import Queueimport threadingimport timeclass WorkManager(object):    def __init__(self, work_num=1000,thread_num=2):        self.work_queue = Queue.Queue()        self.threads = []        self.__init_work_queue(work_num)        self.__init_thread_pool(thread_num)    """        初始化线程    """    def __init_thread_pool(self,thread_num):        for i in range(thread_num):            self.threads.append(Work(self.work_queue))    """        初始化工作队列    """    def __init_work_queue(self, jobs_num):        for i in range(jobs_num):            self.add_job(do_job, i)    """        添加一项工作入队    """    def add_job(self, func, *args):        self.work_queue.put((func, list(args)))#任务入队,Queue内部实现了同步机制    """        检查剩余队列任务    """    def check_queue(self):        return self.work_queue.qsize()    """        等待所有线程运行完毕    """       def wait_allcomplete(self):        for item in self.threads:            if item.isAlive():item.join()class Work(threading.Thread):    def __init__(self, work_queue):        threading.Thread.__init__(self)        self.work_queue = work_queue        self.start()    def run(self):        #死循环,从而让创建的线程在一定条件下关闭退出        while True:            try:                do, args = self.work_queue.get(block=False)#任务异步出队,Queue内部实现了同步机制                do(args)                self.work_queue.task_done()#通知系统任务完成            except Exception,e:                print str(e)                break#具体要做的任务def do_job(args):    print args    time.sleep(0.1)#模拟处理时间    print threading.current_thread(), list(args)if __name__ == '__main__':    start = time.time()    work_manager =  WorkManager(10, 2)    work_manager.wait_allcomplete()    end = time.time()    print "cost all time: %s" % (end-start)

这里,我使用的官网的progressbar组件,打算在多线程中进行使用。

主要方法和属性如下:

currval: current value of the progress, 0 <= currval <= maxvalmaxval: maximum (and final) value of the progressfinished: True if the bar is have finished (reached 100%), False o/wstart_time: first time update() method of ProgressBar was calledseconds_elapsed: seconds elapsed since start_timepercentage(): percentage of the progress (this is a method)

我打算在wait_allcomplete()函数中进行修改

    def wait_for_complete( self ):        for item in self.threads:            if item.isAlive():                item.join()        pbar = ProgressBar(maxval = len(self.threads))        pbar.start()#开始显示进度条        step = 0        i=0        while True:            for td in self.threads:                if td.isAlive() == False:                    step += 1                    pbar.update(step)#进度更新        pbar.finish()

主要思路就是:当有一个线程退出的时候就更新下进度条,但执行的时候,以上代码没能达到预期。
希望大家帮忙看下问题出在哪里

Queue中有个属性:unfinished_tasks,存储了当前未完成的任务数量。
每完成一个任务(task_done),unfinished_tasks的值就会减一。
具体可以查看Queue中的task_done函数。
另外,已经有人将线程池和进度条结合起来了,具体请看starcluster.threadpool
重点看ThreadPool类中的wait函数。


这篇文章你也可以了解下:Queue里task_done方法使用注意

编橙之家文章,

评论关闭