python 3.x - Limiting amount of time a Python3 urllib request takes: timeout not functioning as I expect -
i trying catch unresolved url given urllib request.
import urllib.request def getsite(url): try: urllib.request.urlopen(url, timeout=2) r: print(url, "was resolved!") except: print(url, "wasn't resolved...") return
i expect attempt connection url , if there no response in 2 seconds throws error , prints out isn't resolved. if resolves in under 2 seconds, response accordingly quickly. want happen. i'd each request not last more time prescribe.
as stands, using valid url provides speedy response:
> getsite('http://stackoverflow.com') > http://stackoverflow.com resolved! real 0m0.449s user 0m0.063s sys 0m0.063s
however, using invalid url takes longer 2 seconds:
> getsite('http://thisisntarealwebaddress.com') > http://thisisntarealwebaddress.com wasn't resolved... real 0m18.605s user 0m0.063s sys 0m0.047s
what timeout parameter doing, , how can results want?
docs: https://docs.python.org/3.1/library/urllib.request.html
i solved using run_with_limited_time_function
in this answer , running function like
run_with_limited_time_function(getsite, (url, ), {}, 2)
i'd still hear others have why timeout
doesn't work way expect, though!
copied here sanity:
def run_with_limited_time(func, args, kwargs, time): """runs function time limit :param func: function run :param args: functions args, given tuple :param kwargs: functions keywords, given dict :param time: time limit in seconds :return: true if function ended successfully. false if terminated. """ p = process(target=func, args=args, kwargs=kwargs) p.start() p.join(time) if p.is_alive(): p.terminate() return false return true
Comments
Post a Comment