Python: how can I get a thread to kill itself after a timeout?
I'm writing a multi threaded Python app that makes many TCP connections to servers. Each connection is done on a separate thread. S开发者_运维知识库ometimes the thread hangs for a long time which I don't want. How can I get the thread to kill itself after some given time period? From the main thread how can I determine that the child thread killed itself?
If possible I would appreciate a snippet of code showing how to do this. Thanks.
Update The system is Ubuntu 9:10
Short answer: Just make the def run() end. So, if you are waiting for data from a socket, do it with timeout, then if timeout occur just break the while that you should have, and the thread will be killed.
You can check from main thread if a thread is alive with isAlive() method.
精彩评论