What is the global default timeout
Asked Answered
K

1

54

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?

Kaliningrad answered 15/4, 2015 at 11:36 Comment(0)
V
46

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.

Vacuum answered 15/4, 2015 at 12:0 Comment(4)
so... it looks from Linux kernel?Dramaturgy
That makes for some difficult debugging when you use urllib2.urlopen(link) vs urllib2.urlopen(link, timeout=1). Glad I found this answer though.Squires
@Squires Not really. You're usually catching stderr for logging anyway. If you now realize that you process idles around, you can send a SIGINT and get the latest trace to your log: example = request.urlopen([...]) File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen [...] File "/usr/lib/python3.7/ssl.py", line 1117, in do_handshake self._sslobj.do_handshake() KeyboardInterrupt But I don't think None is a sensible default timeout ...Gurevich
The default timeout might not be None, because socket.setdefaulttimeout() may have been called. So None is the "default default" timeout, but if you call socket.setdefaulttimeout(30), the default timeout is now 30 seconds.Preconcert

© 2022 - 2024 — McMap. All rights reserved.