Basically, I have some threads that MAY block on I/O but has to be stopped in some cases. The entire software architecture is already designed like that, so switching to multi-processing can be painful. Therefore, I searched web and found using thread_ref._Thread__stop()
seems to be the only way that can guarantee to stop a blocked thread.
The problem now is, though that thread is stopped, it is NOT removed from threading.enumerate()
. If I call isAlive()
on its reference, it returns False
. I tried if a thread's run()
method returns normally, that thread should be removed from that list.
This is bad, because if threading still has a reference to that Thread
object, its resources will not be collected, theoretically, and may eventually cause memory leak.
What should I do to make sure things are cleaned up after I do _Thread__stop
on a thread?