什么是全局默认超时 [英] What is the global default timeout

查看:135
本文介绍了什么是全局默认超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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?

推荐答案

我怀疑这取决于实现.也就是说,对于 CPython:

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

来自 socket.create_connection,

如果没有提供 timeout,则使用 :func:getdefaulttimeout 返回的全局默认超时设置.

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

来自 socketmodule.c,

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

之前在同一文件中,

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

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

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.

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

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

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

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

这篇关于什么是全局默认超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆