0

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.

Chris B.
  • 85,731
  • 25
  • 98
  • 139
  • If you have a long running thread, you probably have that running inside some loop right? Add a check inside that loop that if self.__quit, for example, is set to True, you exit the loop. You can overwrite the thread'd join method to set self.__quit to True, ending the loop and thread execution. – Luiz C. Feb 02 '12 at 20:20
  • The potentially long running thread is `self.result = requests.get(url)`. So no, it's not in a loop. – Chris B. Feb 02 '12 at 20:26
  • Then I suppose it would depend on the logic inside of requests.get(url) to return eventually. What is it doing? – jdi Feb 02 '12 at 20:58
  • Making a network call to a url – Chris B. Feb 02 '12 at 20:59
  • Cant the network call time out then? Otherwise, check out this: http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python – jdi Feb 02 '12 at 21:00

1 Answers1

2

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.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13