I have a python 2.7 script on a CentOS linux server which had a bug starting new threads until I could not login to the server anymore.
Is there a way to tell the python runtime not to create/start/use more than 50 threads (and to throw exceptions instead or kill the script) to prevent this problem, or do I have to implement this in the script myself?
The threads are started via thread.start_new_thread().
According to the documentation on the python threading module (the newer threading module) you can call an active_count() method on the module and find out how many threads are running. Now, I understand that you are using the lower-level thread module, so I looked into it by running:
import thread
dir(thread)
This produced the list:
['LockType', '__doc__', '__name__', '__package__', '_count', '_local', 'allocate', 'allocate_lock', 'error', 'exit', 'exit_thread', 'get_ident', 'interrupt_main', 'stack_size', 'start_new', 'start_new_thread']
(I show this because it is very useful in finding out what modules contain, specifically in the interactive terminal)
You can see that the thread module contains a _count field, which when called (e.g. thread._count()) should return the number of threads running (you could check this value and raise exceptions when it exceeds your maximum).
Of course, the underscore in front of _count means that the method is treated, somewhat like a private method. However, in Python you can still access it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With