Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Sometimes 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

like image 249
VacuumTube Avatar asked Jan 25 '11 09:01

VacuumTube


1 Answers

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.

like image 187
webbi Avatar answered Oct 05 '22 14:10

webbi