I'm running a python script which uses threading. I'm creating and starting two threads. Then I check for them to be still active with threading.enumerate()
and for debugging purposes I print the output to the terminal. I'm wondering, why am I getting not only "my threads" but in addition also 4 more which are called: Thread-1, Thread-2, Thread-3, Thread-4.
threading.Thread(target=my_task1.work, name='my_task1').start()
threading.Thread(target=my_tast2.work, name='my_task2').start()
print(threading.active_count())
for thread in threading.enumerate():
print(thread.name)
This outputs the following:
7
MainThread
Thread-1
Thread-2
Thread-3
Thread-4
my_task1
my_task2
What are these 4 threads? I believe, these are kind of system-based threads. If I should ignore them, is there a way I could filter them out in my output list and also only count my "own" threads? I would have expected to only see the Threads I'm actively starting myself and a total count of 3 instead of 7.
I'm running a python script which uses threading. I'm creating and starting two threads. Then I check for them to be still active with threading.enumerate()
and for debugging purposes I print the output to the terminal. I'm wondering, why am I getting not only "my threads" but in addition also 4 more which are called: Thread-1, Thread-2, Thread-3, Thread-4.
threading.Thread(target=my_task1.work, name='my_task1').start()
threading.Thread(target=my_tast2.work, name='my_task2').start()
print(threading.active_count())
for thread in threading.enumerate():
print(thread.name)
This outputs the following:
7
MainThread
Thread-1
Thread-2
Thread-3
Thread-4
my_task1
my_task2
What are these 4 threads? I believe, these are kind of system-based threads. If I should ignore them, is there a way I could filter them out in my output list and also only count my "own" threads? I would have expected to only see the Threads I'm actively starting myself and a total count of 3 instead of 7.
Share Improve this question asked Nov 19, 2024 at 8:46 TetrikusTetrikus 31 silver badge6 bronze badges 2 |1 Answer
Reset to default 3Adding a class Task
to your example code to actually make it run reveals no extra threads like yours. I also added a bit to show the function being run by the thread:
import threading
import time
class Task:
def work(self):
time.sleep(1)
my_task1 = Task()
my_tast2 = Task()
threading.Thread(target=my_task1.work, name="my_task1").start()
threading.Thread(target=my_tast2.work, name="my_task2").start()
print(threading.active_count())
for thread in threading.enumerate():
print(thread.name, getattr(thread, "_target", None))
MainThread None
my_task1 <bound method Task.work of <__main__.Task object at 0x103b24ad0>>
my_task2 <bound method Task.work of <__main__.Task object at 0x103285590>>
This means there's some other code that's being run that you're not showing us that's spawning 4 more threads. You can use the same above _target
trick to maybe reveal what they are. For instance, if you're using a debugger, it would likely have started a few threads.
is there a way I could filter them out in my output list and also only count my "own" threads
Well, you're already naming your own threads in a pretty clear way, so sure:
my_threads = [t for t in threading.enumerate() if t.name.startswith("my_task")]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745572332a4633754.html
threading.enumerate
says that it can return "dummy"Thread
objects corresponding to threads that were created by means other thanthreading.Thread(...)
calls. It doesn't say anything about how those other threads could be created, but I'm pretty sure that, for example, if your program usesconcurrent.futures
or if your program uses amultiprocessing.pool.ThreadPool
then you can get "dummies" that way. Your example code is not a complete program. What calls your example code? – Solomon Slow Commented Nov 19, 2024 at 13:44import
ed by your program. @AKX embedded your five lines of code in a complete program that does not behave the way that you described. What happens when you run AKX's program in your Python environment? – Solomon Slow Commented Nov 19, 2024 at 13:47