【问题标题】:How to get the number of active threads started by specific class?如何获取特定类启动的活动线程数?
【发布时间】:2010-10-28 20:37:33
【问题描述】:

代码如下:

class workers1(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
        # ...do some stuff 

class workers2(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
       # ...do some stuff 


if __name__ == "__main__":
    # start workers

while True: 
    print "Number of threads active", threading.activeCount()
    print "Number of worker1 threads", ?????, "Number of worker2 threads", ?????

有没有办法通过原始类获取活动线程数?

【问题讨论】:

    标签: python multithreading count


    【解决方案1】:

    这是对 Doug Hellman 的 multiprocessing ActivePool example code 的小修改(使用线程)。这个想法是让您的工作人员在池中注册自己,完成后取消注册,使用 threading.Lock 来协调修改池的活动列表:

    import threading
    import time
    import random
    
    class ActivePool(object):
        def __init__(self):
            super(ActivePool, self).__init__()
            self.active=[]
            self.lock=threading.Lock()
        def makeActive(self, name):
            with self.lock:
                self.active.append(name)
        def makeInactive(self, name):
            with self.lock:
                self.active.remove(name)
        def numActive(self):
            with self.lock:
                return len(self.active)
        def __str__(self):
            with self.lock:
                return str(self.active)
    def worker(pool):
        name=threading.current_thread().name
        pool.makeActive(name)
        print 'Now running: %s' % str(pool)
        time.sleep(random.randint(1,3))
        pool.makeInactive(name)
    
    if __name__=='__main__':
        poolA=ActivePool()
        poolB=ActivePool()    
        jobs=[]
        for i in range(5):
            jobs.append(
                threading.Thread(target=worker, name='A{0}'.format(i),
                                 args=(poolA,)))
            jobs.append(
                threading.Thread(target=worker, name='B{0}'.format(i),
                                 args=(poolB,)))
        for j in jobs:
            j.daemon=True
            j.start()
        while threading.activeCount()>1:
            for j in jobs:
                j.join(1)
                print 'A-threads active: {0}, B-threads active: {1}'.format(
                    poolA.numActive(),poolB.numActive())
    

    产量

    Now running: ['A0']
    Now running: ['B0']
    Now running: ['A0', 'A1']
    Now running: ['B0', 'B1']
     Now running: ['A0', 'A1', 'A2']
     Now running: ['B0', 'B1', 'B2']
    Now running: ['A0', 'A1', 'A2', 'A3']
    Now running: ['B0', 'B1', 'B2', 'B3']
    Now running: ['A0', 'A1', 'A2', 'A3', 'A4']
    Now running: ['B0', 'B1', 'B2', 'B3', 'B4']
    A-threads active: 4, B-threads active: 5
    A-threads active: 2, B-threads active: 5
    A-threads active: 0, B-threads active: 3
    A-threads active: 0, B-threads active: 3
    A-threads active: 0, B-threads active: 3
    A-threads active: 0, B-threads active: 3
    A-threads active: 0, B-threads active: 3
    A-threads active: 0, B-threads active: 0
    A-threads active: 0, B-threads active: 0
    A-threads active: 0, B-threads active: 0
    

    【讨论】:

      【解决方案2】:

      您可以为每个类使用信号量并获取它们的计数:请参阅link

      【讨论】:

      • 链接失效。请避免引用链接作为您的全部答案。任何东西都是可取的——包括复制粘贴该链接的内容以进行历史保存,如果你绝对必须的话。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2010-12-15
      • 2011-09-28
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多