Chris B.
Chris B.

Reputation: 90289

How can I ensure a hung Thread eventually gets garbage collected in Python

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:

  1. When the thread terminates, or
  2. 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.

Upvotes: 0

Views: 504

Answers (1)

Lawrence D'Oliveiro
Lawrence D'Oliveiro

Reputation: 2804

I don’t know why you’re bothering with a separate thread, given that your main thread is blocked while waiting for the request to complete or timeout, anyway. Why not simply have the request timeout?

Note that you can call Socket.setdefaulttimeout to impose a default timeout on Socket objects created from that point on, e.g. in libraries which do not themselves give you an option for specifying a request timeout.

Upvotes: 2

Related Questions