Python 什么是全局默认超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29649173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:50:22  来源:igfitidea点击:

What is the global default timeout

pythonrequesttimeouturllib

提问by user3139774

Python 3.4 . Trying to find what is the default timeout in urllib.request.urlopen() .

蟒蛇 3.4 。试图找到 urllib.request.urlopen() 中的默认超时时间。

Its signature is: urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

它的签名是: urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

The docsays that its "global default timeout", and looking at the code its: socket._GLOBAL_DEFAULT_TIMEOUT

文档说它的“全局默认超时”,并查看其代码:socket._GLOBAL_DEFAULT_TIMEOUT

Still what is the actual value in secs?

仍然以秒为单位的实际值是多少?

采纳答案by Kevin

I suspect this is implementation-dependent. That said, for CPython:

我怀疑这是依赖于实现的。也就是说,对于 CPython:

From socket.create_connection,

socket.create_connection,

If no timeoutis supplied, the global default timeout setting returned by :func:getdefaulttimeoutis used.

如果没有超时供应,全局默认超时设置返回由:FUNC:getdefaulttimeout使用。

From socketmodule.c,

socketmodule.c,

static PyObject *
socket_getdefaulttimeout(PyObject *self)
{
    if (defaulttimeout < 0.0) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    else
        return PyFloat_FromDouble(defaulttimeout);
}

Earlierin the same file,

在同一个文件的前面

static double defaulttimeout = -1.0; /* Default timeout for new sockets */

So it looks like Py_None, aka None, is the default timeout. In other words, urlopennever times out. At least not from the Python end. I guess a timeout can still occur if the networking functions supplied by the OS have timeouts themselves.

所以看起来Py_None,又名None,是默认超时。换句话说,urlopen永远不会超时。至少不是来自 Python 端。我想如果操作系统提供的网络功能本身有超时,仍然会发生超时。



Edit: oops, I guess I didn't need to go source diving for the answer at all, since it's right there in the docs.

编辑:哎呀,我想我根本不需要去寻找答案的源头,因为它就在文档中

A value of Noneindicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

None表示新的套接字对象没有超时。首次导入 socket 模块时,默认为None.