What is the global default timeout

ε祈祈猫儿з 提交于 2019-12-18 01:43:07

问题


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

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

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

Still what is the actual value in secs?


回答1:


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

From socket.create_connection,

If no timeout is supplied, the global default timeout setting returned by :func:getdefaulttimeout is used.

From socketmodule.c,

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

Earlier in 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, urlopen never 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.


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 None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.



来源:https://stackoverflow.com/questions/29649173/what-is-the-global-default-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!