Limit count of threads for python script
Question: 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()`.


Answer: According to the [documentation on the python `threading`
module](http://docs.python.org/2/library/threading.html#module-threading) (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.

