什么是全局默认超时

use*_*774 25 python timeout urllib request

Python 3.4.试图找到urllib.request.urlopen()中的默认超时.

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

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

秒的实际价值是多少?

Kev*_*vin 27

我怀疑这是依赖于实现的.那说,对于CPython:

来自socket.create_connection,

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

来自socketmodule.c,

static PyObject *
socket_getdefaulttimeout(PyObject *self)
{
    if (defaulttimeout < 0.0) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    else
        return PyFloat_FromDouble(defaulttimeout);
}
Run Code Online (Sandbox Code Playgroud)

在同一档案的早些时候,

static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Run Code Online (Sandbox Code Playgroud)

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


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

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