D
David Poundall
I have a thread class and I want to be able to track its usage within
an application. FYI the class launches aplications in their own thread
when the 'launch' method is called.
That works OK
However I want to take things a bit further and my thinking was that I
could use a class parameter to do a count but I am having no luck I am
afraid.
Its 'GlobalThreadCount' I am trying to get to work. I may need to use
the globals built in - or do I.
Thanks in advance
David
#--------------------------------------------------------
from threading import Thread, Event
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):
self.Threads = {}
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
GlobalThreadCount += 1
def globalusagecount(self):
"""
Returns a count figure for how many
threads are running in total
using this class.
"""
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
an application. FYI the class launches aplications in their own thread
when the 'launch' method is called.
That works OK
However I want to take things a bit further and my thinking was that I
could use a class parameter to do a count but I am having no luck I am
afraid.
Its 'GlobalThreadCount' I am trying to get to work. I may need to use
the globals built in - or do I.
Thanks in advance
David
#--------------------------------------------------------
from threading import Thread, Event
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):
self.Threads = {}
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
GlobalThreadCount += 1
def globalusagecount(self):
"""
Returns a count figure for how many
threads are running in total
using this class.
"""
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