473,811 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting Threads

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 'GlobalThreadCo unt' 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
"""

GlobalThreadCou nt = 0

def __init__(self):
self.Threads = {}

def launch(self, SubToLaunch, SubsArgs=(),
SubsKwargs={}, AsDeamon=True):
t = Thread(target=S ubToLaunch, args = SubsArgs,
kwargs = SubsKwargs)
t.setDaemon(AsD eamon)
t.start()
self.Threads[len(self.Thread s)] = t
# Stash the thread in an instance local

GlobalThreadCou nt += 1

def globalusagecoun t(self):
"""
Returns a count figure for how many
threads are running in total
using this class.
"""
return GlobalThreadCou nt
def stop(self,Threa dID):
t = self.Threads[ThreadID]
t.stop()
self.Threads.cl ear[ThreadID]

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

def runningkeys(sel f):
"""
Returns a llist of all running Keys
"""
return self.Threads.ke ys

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.app end(t)
return ThreadsDown

Oct 27 '05 #1
9 1884
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
"""

GlobalThreadCou nt = 0

def __init__(self):
global GlobalThreadCou nt
self.Threads = {}
GlobalThreadCou nt = 0
def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
AsDeamon=True):
t = Thread(target=S ubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsD eamon)
t.start()
self.Threads[len(self.Thread s)] = t # Stash the
thread in an instance local

global GlobalThreadCou nt
GlobalThreadCou nt += 1
print GlobalThreadCou nt

def globalusagecoun t(self):
"""
Returns a count figure for how many threads are running in
total
usnig this class.
"""
global GlobalThreadCou nt
return GlobalThreadCou nt
def stop(self,Threa dID):
t = self.Threads[ThreadID]
t.stop()
self.Threads.cl ear[ThreadID]

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

def runningkeys(sel f):
"""
Returns a llist of all running Keys
"""
return self.Threads.ke ys

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.app end(t)
return ThreadsDown

Oct 27 '05 #2
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
"""

GlobalThreadCou nt = 0

def __init__(self):
global GlobalThreadCou nt
self.Threads = {}
GlobalThreadCou nt = 0
def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
AsDeamon=True):
t = Thread(target=S ubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsD eamon)
t.start()
self.Threads[len(self.Thread s)] = t # Stash the
thread in an instance local

global GlobalThreadCou nt
GlobalThreadCou nt += 1
print GlobalThreadCou nt

def globalusagecoun t(self):
"""
Returns a count figure for how many threads are running in
total
usnig this class.
"""
global GlobalThreadCou nt
return GlobalThreadCou nt
def stop(self,Threa dID):
t = self.Threads[ThreadID]
t.stop()
self.Threads.cl ear[ThreadID]

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

def runningkeys(sel f):
"""
Returns a llist of all running Keys
"""
return self.Threads.ke ys

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.app end(t)
return ThreadsDown

Oct 27 '05 #3
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
"""

GlobalThreadCou nt = 0
Style note: using MixedCaps for variable names doesn't fit conventional
Python style. To be conventional you would use "globalThreadCo unt"
here, or just "threadCoun t" since it's no more "global" than any other
class attribute.
def __init__(self):
global GlobalThreadCou nt
And here you definitely don't want "global" since your GlobalThreadCou nt
above is *not* a Python "global". It's a class attribute, and you
should reference it as such:
self.Threads = {}
GlobalThreadCou nt = 0 Change to:
ServerThreads.G lobalThreadCoun t = 0

and access it as such in all other places inside the class.
def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
AsDeamon=True):
t = Thread(target=S ubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsD eamon)
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.Thread s)] = t # Stash the
thread in an instance local

global GlobalThreadCou nt
GlobalThreadCou nt += 1
print GlobalThreadCou nt
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 globalusagecoun t(self):
"""
Returns a count figure for how many threads are running in
total
usnig this class.
"""
global GlobalThreadCou nt
return GlobalThreadCou nt


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
Oct 27 '05 #4
>> 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...
On re-reading it - neither do I. Sorry about that. What I meant to
say was that I was after an increment in the count variable (the class
attribute) not an increment only within the class instances. Because
the variable was being set to zero every time the class was
instantiated I wasn't getting the true usage count.
ServerThreads.G lobalThreadCoun t = 0
That was what the syntax I was missing.
You appear to know there is a difference in spelling between these two
"demons"... but only "daemon" is correct. "Deamon" is a misspelling.
That one certainly slipped under the radar.
You would want to wrap it with a threading.Lock( ) to
protect it in that case.
Will do.
But, again, using a global at all is wrong anyway


I posted using this sytax as it was nearly working - but I knew there
was a serious clanger in the code and you pointed it out when you
identified it was the class attribute syntax I was after.

Peter, thank you for taking the time to unpick my code. It's much
apreciated. I have only been coding with Python for the past couple of
weeks and switching my brain to work with Python classes has been a bit
of a challenge. I wil re-read the document section on style tips
before I get too far into my app, as I would really like to be able to
code in a Pythonesque fashion.

David.

Oct 27 '05 #5
First time I have used thread locking. This seems to work but is it
correct?

from threading import Thread, Event, Lock
..
..
..
def launch(self, ThreadTitle, SubToLaunch, SubsArgs=(),
SubsKwargs={}, AsDaemon=True):
my_lock = Lock()
my_lock.acquire ()
try:
# ---------------------
t = Thread(target=S ubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsD aemon)
t.start()
self.Threads[len(self.Thread s)] = t # Stash the
thread in an instance local
# ---------------------
ServerThreads.t hreadcount += 1
finally:
my_lock.release ()
..
..
..

Oct 27 '05 #6
On 27 Oct 2005 16:38:36 -0700, "David Poundall" <da***@jotax.co m>
declaimed the following in comp.lang.pytho n:
First time I have used thread locking. This seems to work but is it
correct?
Not quite, since you may also want to lock it around the print
option too...

Create the lock just like you did the counter (CLASS level), then
reference it using the class name prefix in all places (you should only
need the acquire/release pair in those places, but put them around all
uses of the counter).
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Oct 28 '05 #7
Sorry Denis - but could you give me an example. I can't for the life
of me see how the syntax for that would go.

Oct 28 '05 #8
On 28 Oct 2005 00:21:28 -0700, "David Poundall" <da***@jotax.co m>
declaimed the following in comp.lang.pytho n:
Sorry Denis - but could you give me an example. I can't for the life
of me see how the syntax for that would go.
I'm having to recreate your code from snippets posted over the past
few messages...

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

GlobalThreadCou nt = 0
CounterLock = threading.Lock( )

def __init__(self):
self.Threads = {}
# GlobalThreadCou nt = 0 ## unneeded here
def launch(self, SubToLaunch, SubsArgs=(), SubsKwargs={},
AsDeamon=True):
t = Thread(target=S ubToLaunch, args = SubsArgs, kwargs =
SubsKwargs)
t.setDaemon(AsD eamon)
t.start()
self.Threads[len(self.Thread s)] = t # Stash the
thread in an instance local

# global GlobalThreadCou nt ## not a global
ServerThreads.C ounterLock.acqu ire()
ServerThreads.G lobalThreadCoun t += 1
print ServerThreads.G lobalThreadCoun t
ServerThreads.C ounterLock.rele ase()

def globalusagecoun t(self):
"""
Returns a count figure for how many threads are running in
total
usnig this class.
"""
# global GlobalThreadCou nt ##not global
ServerThreads.C ounterLock.acqu ire()
tmp = ServerThreads.G lobalThreadCoun t
ServerThreads.C ounterLock.rele ase()
return tmp ##GlobalThreadC ount
def stop(self,Threa dID):
t = self.Threads[ThreadID]
t.stop()
self.Threads.cl ear[ThreadID]

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

def runningkeys(sel f):
"""
Returns a llist of all running Keys
"""
return self.Threads.ke ys

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.app end(t)
return ThreadsDown

Hmmmm, I notice you never decrement the counter...
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Oct 28 '05 #9
After much optimisation it turns out the following code does the job
for me. In the end using count didn't give me the flexibility I
needed. Instead I now name each thread and track them accordingly.
It's arguable if I need the thread locking now though, however I have
left it in to remind me of the syntax.

Thank you for posting back Dennis. Much appreciated.

# --------------------------------------------------------
class ServerThreads:
"""
Wrapper for thread handling. These threads are not
dynamic, that is to say once the application is fully
loaded the number of threads running is determined
by the size of the application - and is fixed.
"""
# --------------------- Class Attributes
# Dict holds object references to all threads
# created in the server (running or not)
thr_objects = {}
# Dict holds status of all running threads
# 0 = stopped, 1 = running
thr_running = {}
# Lock object
lck = Lock()

def launch(self, ThrName, SubToLaunch,
SubsArgs=(), SubsKwargs={}, AsDaemon=True):

""" Kickoff a thread.
thr_objects : Dictionary using ThreadTitle
as the key which holds
references to the thread object
thr_running : Dictionary holding the status
of each thread
"""
s = ServerThreads
# ---------------------
s.lck.acquire()
try:
t = Thread(name = ThrName, target=SubToLau nch,
args = SubsArgs, kwargs = SubsKwargs)
t.setDaemon(AsD aemon) # Must be set before start
s.thr_objects[ThrName] = t
s.thr_running[ThrName] = 1
if ag.gb_Appdebug: print 'Thread Started ------------- ',
ThrName
t.start()
finally:
s.lck.release()
# ---------------------

def stoprequest(sel f,thr_name):
""" Thread stop request - stop is pending.
Join is not needed because the main code body
drops out of the thread loops once the
thr_running = true condition has been removed."""
s = ServerThreads
# ---------------------
s.lck.acquire()
s.thr_running[thr_name] = 0 # Flag to tell running
thread to please terminate
if ag.gb_Appdebug: print 'Thread Stopping ----------- ' +
thr_name
s.lck.release()
# ---------------------

def allOK(self):
""" Returns a list of all threads that are down
when they shouldn't be (if any) """
s = ServerThreads
ThreadsDown = []
for thr_name in s.thr_objects:
if not s.thr_objects[thr_name].isAlive() and
s.thr_running[thr_name]:
# If a thread has an unscheduled stop indicate this by
returning the
# threads name otherwise return None.
ThreadsDown.app end(thr_name)
return ThreadsDown

Oct 29 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2186
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
1
2712
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last client deletes a refernce to it
32
4235
by: William Stacey [MVP] | last post by:
Here is the link. If you find an issue or think of a feature, please post a reply or send an email. Cheers! http://www.mvptools.com/doco/csharp/semaphoredjikstra.htm -- William Stacey, MVP
1
6934
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
4
4199
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { } double &operator()(int i) { return myX; }
10
1520
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short lived threads that get a request and return a reply and end. Can the main thread tell me how many connection threads are currently running at any given time? I'd like to have a label on the main form to show how many connections the server is...
190
8215
by: blangela | last post by:
If you had asked me 5 years ago about the future of C++, I would have told you that its future was assured for many years to come. Recently, I have been starting to wonder. I have been teaching C++ at a local polytechnical school here in Vancouver, Canada for approximately 8 years. Six years ago, at the height (or should I say volume?) of the internet bubble, I had 80+ students per semester in my C++ course. Now I am fortunate to have...
0
1370
by: Chris Thomasson | last post by:
Here are the pre-alpha code downloads: http://appcore.home.comcast.net/vzoom/refcount/ (both C and C++ api here...) Here is some pre-alpha documentation: http://appcore.home.comcast.net/vzoom/refcount/doc/
7
1412
by: grabit | last post by:
Hi Peoples I have the following query to retrieve data from the db. 2 of the things it has to do is give me the date of the last post and a count of the post records in each of the categories. The last post date works fine but i cannot get the COUNT(threadID) to return anything but 1 record for each category. Can someone PLEASE help query is as follows: --------------------------- <cfquery name="showcategories" datasource="#dsn#"> ...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.