华子的代码空间

逆水行舟,不进则退。 关注系统编程、网络编程、并发、分布式。

python监视线程池

先上代码:

#!/usr/bin/python
import threading
import time

class Monitor(threading.Thread):
    def __init__(self, *args,**kwargs):
        super(Monitor,self).__init__()
        self.daemon = False
        self.args = args
        self.kwargs = kwargs
        self.pool_list = []
    
    def run(self):
        print self.args
        print self.kwargs
        for name,value in self.kwargs.items():
            obj = value[0]
            temp = {}
            temp[name] = obj
            self.pool_list.append(temp)
        while 1:
            print self.pool_list
            for name,value in self.kwargs.items():
                obj = value[0]
                parameters = value[1:]
                died_threads = self.cal_died_thread(self.pool_list,name)
                print "died_threads", died_threads
                if died_threads >0:
                    for i in range(died_threads):
                        print "start %s thread..." % name
                        t = obj[0].__class__(*parameters)
                        t.start()
                        self.add_to_pool_list(t,name)
                else:
                    break
            time.sleep(0.5)

    def cal_died_thread(self,pool_list,name):
        i = 0
        for item in self.pool_list:
            for k,v in item.items():
                if name == k:
                    lists = v
        for t in lists:
            if not t.isAlive():
                self.remove_from_pool_list(t)
                i +=1
        return i
    
    def add_to_pool_list(self,obj,name):
        for item in self.pool_list:
            for k,v in item.items():
                if name == k:
                    v.append(obj)
    
    def remove_from_pool_list(self, obj):
        for item in self.pool_list:
            for k,v in item.items():
                try:
                    v.remove(obj)
                except:
                    pass
                else:
                    return

 

使用方法:

    rrds_queue = Queue.Queue()
    
    make_rrds_pool = []
    for i in range(5):
        make_rrds_pool.append(MakeRrds(rrds_queue))
    for i in make_rrds_pool:
        i.start()
    
    
    make_graph_pool = []
    for i in range(5):
        make_graph_pool.append(MakeGraph(rrds_queue))
    for i in make_graph_pool:
        i.start()
    
    
    monitor = Monitor(make_rrds_pool=(make_rrds_pool, rrds_queue), \
                      make_graph_pool=(make_graph_pool, rrds_queue))
    monitor.start()

 

解析:

1. 接受字典参数,value为一个元组,第一个元素是线程池,后面的都是参数。

2. 每0.5秒监视线程池中的线程数量,如果线程死掉了,记录死掉线程的数目,再启动同样数量的线程。

3. 如果没有线程死去,则什么也不做。

posted on 2012-08-03 13:18  华子的代码空间  阅读(1242)  评论(0编辑  收藏  举报

导航