2010-10-28 6 views
7

कोड के नीचे दिखाई देता है:पायथन - विशिष्ट वर्ग द्वारा सक्रिय सक्रिय धागे की संख्या कैसे प्राप्त करें?

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", ????? 

वहाँ एक रास्ता धागे उद्भव वर्ग द्वारा सक्रिय होने की संख्या प्राप्त करने है?

उत्तर

11

यह डौग हेलमैन के 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 
3

आप प्रत्येक कक्षा के लिए एक सेमफोर का उपयोग कर सकते हैं और उनकी गणना प्राप्त कर सकते हैं: link देखें।

+0

कि उपयोगी है, धन्यवाद – m1k3y3

संबंधित मुद्दे