Counting Threads
David Poundall
david at jotax.com
Thu Oct 27 13:56:32 EDT 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={},
AsDeamon=True):
t = Thread(target=SubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsDeamon)
t.start()
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
total
usnig this class.
"""
global GlobalThreadCount
return GlobalThreadCount
def stop(self,ThreadID):
t = self.Threads[ThreadID]
t.stop()
self.Threads.clear[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:
ThreadsDown.append(t)
return ThreadsDown
More information about the Python-list
mailing list