搜索

首页  >  问答  >  正文

Python:在多线程中使用进度条(progressbar)碰到的问题

Python线程池代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<code># !/usr/bin/env python

# -*- coding:utf-8 -*-

# ref_blog:http://www.open-open.com/home/space-5679-do-blog-id-3247.html

 

import Queue

import threading

import time

 

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

</code>

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

主要方法和属性如下:

  • currval: current value of the progress, 0 <= currval <= maxval
  • maxval: maximum (and final) value of the progress
  • finished: True if the bar is have finished (reached 100%), False o/w
  • start_time: first time update() method of ProgressBar was called
  • seconds_elapsed: seconds elapsed since start_time
  • percentage(): percentage of the progress (this is a method)

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

</code>

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

大家讲道理大家讲道理2902 天前1147

全部回复(1)我来回复

  • 迷茫

    迷茫2017-04-17 13:30:24

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


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

    回复
    0
  • 取消回复