Python: how can I get a thread to kill myself after a timeout?

I am writing a multi-threaded Python application that makes many TCP connections on servers. Each connection is performed in a separate thread. Sometimes the thread hangs for a long time, which I do not want. How can I make a thread kill itself after a certain period of time? From the main thread, how can I determine if the child thread killed itself?

If possible, I would appreciate a piece of code showing how to do this. Thanks.

Ubuntu System Update 9:10

+6
python multithreading
source share
1 answer

Short answer: just do the def run () ending. So, if you are waiting for data from a socket, do it with a timeout, then if a timeout occurs, just add up the time you should have and the thread will be killed.

You can check from the main thread if the thread is alive using the isAlive () method.

+4
source share

All Articles