Counting Threads

Peter Hansen peter at engcorp.com
Thu Oct 27 15:19:45 EDT 2005


David Poundall wrote:
> 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.  

I have no idea what that last sentence above means...

> Instead
> I get a count based on the instance usage.

> Idea's anyone?

Several, interspersed below:

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

Style note: using MixedCaps for variable names doesn't fit conventional 
Python style.  To be conventional you would use "globalThreadCount" 
here, or just "threadCount" since it's no more "global" than any other 
class attribute.

>     def __init__(self):
>         global GlobalThreadCount

And here you definitely don't want "global" since your GlobalThreadCount 
above is *not* a Python "global".  It's a class attribute, and you 
should reference it as such:

>         self.Threads = {}
>         GlobalThreadCount = 0
Change to:
           ServerThreads.GlobalThreadCount = 0

and access it as such in all other places inside the class.

>     def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
> AsDeamon=True):
>         t = Thread(target=SubToLaunch, args = SubsArgs, kwargs =
> SubsKwargs)
>         t.setDaemon(AsDeamon)

You appear to know there is a difference in spelling between these two 
"demons"... but only "daemon" is correct.  "Deamon" is a misspelling.

>         t.start()
>         self.Threads[len(self.Threads)] = t           # Stash the
> thread in an instance local
> 
>         global GlobalThreadCount
>         GlobalThreadCount += 1
>         print  GlobalThreadCount

Caution: if you can't guarantee that launch() will be called from only 
one thread, you have a race condition here since the count could be 
increased "simultaneously" from multiple threads and you won't get 
reliable results.  You would want to wrap it with a threading.Lock() to 
protect it in that case.

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

Style/usage note: although as I mentioned you don't want to use "global" 
  variables anyway, even if you did the above line is unnecessary.  You 
only need to use "global" if you are going to *rebind* the global name 
(i.e. assign a new value to it), not if you are merely reading the value 
and returning it, as above.  What you wrote would work, it's just not 
needed or usual.  (But, again, using a global at all is wrong anyway.)


-Peter



More information about the Python-list mailing list