Alex Alex - 2 months ago 11
Python Question

Why does the python threading.Thread object has 'start', but not 'stop'?

The python module threading has an object

Thread
to be used to run processes and functions in a different thread. This object has a
start
method, but no
stop
method. What is the reason a
Thread
cannot be stopped my calling a simple
stop
method? I can imagine cases when it is unconvenient to use the
join
method...

Answer

start can be generic and make sense because it just fires off the target of the thread, but what would a generic stop do? Depending upon what your thread is doing, you could have to close network connections, release system resources, dump file and other streams, or any number of other custom, non-trivial tasks. Any system that could do even most of these things in a generic way would add so much overhead to each thread that it wouldn't be worth it, and would be so complicated and shot through with special cases that it would be almost impossible to work with. You can keep track of all created threads without joining them in your main thread, then check their run state and pass them some sort of termination message when the main thread shuts itself down though.