限制Python3 urllib请求所花费的时间:超时不能按我的预期运行

问题描述 投票:1回答:1

我试图抓住一个未解决的url给urllib请求。

import urllib.request

def getSite(url):
    try:
        with urllib.request.urlopen(url, timeout=2) as r:
            print(url, "was resolved!")
    except:
        print(url, "wasn't resolved...")
    return

我希望这会尝试连接到url,如果在2秒内没有响应,它会抛出错误并打印出它没有被解析。如果它在2秒内解决,它会相应地快速响应。这就是我想要发生的事情。我希望每个请求的持续时间不超过我开的时间。

就目前而言,使用有效的网址可以提供快速响应:

> getSite('http://stackoverflow.com')

> http://stackoverflow.com was resolved!
    real    0m0.449s
    user    0m0.063s
    sys     0m0.063s

但是,使用无效的URL需要的时间超过2秒:

> getSite('http://thisisntarealwebaddress.com')

> http://thisisntarealwebaddress.com wasn't resolved...
    real    0m18.605s
    user    0m0.063s
    sys     0m0.047s

什么是超时参数真正做的,我怎样才能得到我想要的结果?

文件:https://docs.python.org/3.1/library/urllib.request.html

python-3.x timeout httprequest urllib
1个回答
0
投票

我通过在run_with_limited_time_function中使用this answer并运行我的函数来解决这个问题

run_with_limited_time_function(getSite, (url, ), {}, 2)

我还是想听听别人怎么说有关为什么timeout不按照我的预期工作的原因!


复制在这里是为了理智:

def run_with_limited_time(func, args, kwargs, time):
    """Runs a function with time limit

    :param func: The function to run
    :param args: The functions args, given as tuple
    :param kwargs: The functions keywords, given as dict
    :param time: The time limit in seconds
    :return: True if the function ended successfully. False if it was terminated.
    """
    p = Process(target=func, args=args, kwargs=kwargs)
    p.start()
    p.join(time)
    if p.is_alive():
        p.terminate()
        return False

    return True
© www.soinside.com 2019 - 2024. All rights reserved.