I've got a long running program which periodically makes connections to external network resources. I've wrapped those calls in a timeout thread, so if the call takes more than 10 seconds, the call returns an error immediately, like so:
def fetch_resource(url):
class TimeoutThread(Thread):
def __init__(self):
Thread.__init__(self)
self.result = None
def run(self):
self.result = requests.get(url)
tt = TimeoutThread()
tt.start()
tt.join(10.0)
if tt.result:
return tt.result.content
else:
return None
Reading the Thread documentation, however, it appears Thread.join()
will return either:
- When the thread terminates, or
- In 10 seconds
In the case where .join()
returns in 10 seconds, the thread is still alive, correct? I understand there are no good ways to kill the thread, but how do I ensure the thread eventually terminates and gets garbage collected? I'm worried that threads might hang and never return, thus gradually eating up resources which never get freed.