4

I am trying to download a URL using python-wget downloaded from : https://pypi.python.org/pypi/wget

This package does not support a timeout option, hence it takes some time (around 10seconds) roughly for a query to fail. Is it possible to add a timeout in our try block to lessen the wait time for a function.

Something like this:

try(timeout=5s):
     wget.download(URL)
except:
    print "Query timed out"
ashdnik
  • 648
  • 3
  • 8
  • 20

1 Answers1

4

The easiest way (i.e. if download doesn't support timeout and you can't modify the code) to achieve that is by running the code in another thread:

from threading import Thread

t = Thread(target=wget.download, args=(URL,))
t.daemon = True
t.start()
t.join(5)
if t.is_alive():
    print 'Timeout'
freakish
  • 54,167
  • 9
  • 132
  • 169