Counting Threads

David Poundall david at
Thu Oct 27 19:56:36 CEST 2005

Just sorted (its always the way when you post isn't it)

But the count isn't doint what I expected.  I was after a increment
after every time the class was used ireespective of instance.  Instead
I get a count based on the instance usage.

Idea's anyone?

class ServerThreads:
    Wrapper for thread handling.
    Thread ID 0 = Controller
    Thread ID 1 = Plant Object 1
    Thread ID 2 = Plant Object 2

    GlobalThreadCount = 0

    def __init__(self):
        global GlobalThreadCount
        self.Threads = {}
        GlobalThreadCount = 0

    def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
        t = Thread(target=SubToLaunch, args = SubsArgs, kwargs =
        self.Threads[len(self.Threads)] = t           # Stash the
thread in an instance local

        global GlobalThreadCount
        GlobalThreadCount += 1
        print  GlobalThreadCount

    def globalusagecount(self):
        Returns a count figure for how many threads are running in
        usnig this class.
        global GlobalThreadCount
        return GlobalThreadCount

    def stop(self,ThreadID):
        t = self.Threads[ThreadID]

    def count(self):
        Returns alist of how many threads are running in the instance
        return len(self.Threads)

    def runningkeys(self):
        Returns a llist of all running Keys
        return self.Threads.keys

    def allOK(self):
        Returns a list of all threads that are down  if any)
        ThreadsDown = []
        for t in self.Threads:
            if not self.Threads[t].isAlive:
        return ThreadsDown

More information about the Python-list mailing list