listing threads?
Peter Hansen
peter at engcorp.com
Thu Feb 28 19:32:31 EST 2002
maximilianscherr wrote:
>
> in the docs theres the information, that i can list all active
> threads with threading.enumerate(), but i always just get the
> Mainthread displayed in the interpreter console.
>
> im doing somethin like:
[...snip]
> then if i do threading.enumerate() i just get the mainthread int he
> list.
>
> so how can i now list all (active) threads?
Just as you are trying to do.
Does this help?
>>> class test(t.Thread):
... def run(self):
... print 'hi'
... self.terminated = 0
... import time
... while not self.terminated:
... time.sleep(1)
... print 'done'
...
>>> te = test()
>>> te.start()
hi
>>> t.enumerate()
[<test(Thread-2, started)>, <_MainThread(MainThread, started)>]
>>> te.terminated = 1
>>> done
>>> t.enumerate()
[<_MainThread(MainThread, started)>]
>>>
Your problem is that (a) you don't have a run() method in
your thread class, and that is what gets executed when you
call te.start(), and (b) your thread is therefore terminating
immediately upon creation, and therefore t.enumerate()
is returning only the main thread, because no others are
running.
-Peter
More information about the Python-list
mailing list