Below are two simple python functions. First, it tries to connect to test.com
the 666 domain (the host name is valid, but the port is not). The second tries to connect to imap.mail.outlook.com
on port 993 (the host name is valid, but does not look for public use / access).
def fn_outlook(timeout):
try:
socket.create_connection(("imap.mail.outlook.com", 993), timeout=timeout)
except socket.timeout:
pass
def fn_test(timeout):
try:
socket.create_connection(("test.com", 666), timeout=timeout)
except socket.timeout:
pass
And here is the runtime for this function with different timeouts:
In [14]: %time fn_test(1)
CPU times: user 644 µs, sys: 1.07 ms, total: 1.71 ms
Wall time: 1 s
In [15]: %time fn_test(2)
CPU times: user 589 µs, sys: 1.15 ms, total: 1.74 ms
Wall time: 2 s
In [16]: %time fn_outlook(2)
CPU times: user 838 µs, sys: 2.24 ms, total: 3.08 ms
Wall time: 7.15 s
In [17]: %time fn_outlook(4)
CPU times: user 705 µs, sys: 1.18 ms, total: 1.88 ms
Wall time: 12 s
In [18]: %time fn_test(4)
CPU times: user 483 µs, sys: 795 µs, total: 1.28 ms
Wall time: 4.42 s
In case, the test.com
connection will be disconnected after the same time as in the argument timeout
. But for imap.mail.outlook.com
everything, it becomes interesting - socket connections ignore the timeout argument. To be precise, do not ignore, but rather always connect timeouts after some longer period of time.
, imap.mail.outlook.com
, .