Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11ba094

Browse files
committed
Major overhaul of timeout sockets:
- setblocking(0) and settimeout(0) are now equivalent, and ditto for setblocking(1) and settimeout(None). - Don't raise an exception from internal_select(); let the final call report the error (this means you will get an EAGAIN error instead of an ETIMEDOUT error -- I don't care). - Move the select to inside the Py_{BEGIN,END}_ALLOW_THREADS brackets, so other theads can run (this was a bug in the original code). - Redid the retry logic in connect() and connect_ex() to avoid masking errors. This probably doesn't work for Windows yet; I'll fix that next. It may also fail on other platforms, depending on what retrying a connect does; I need help with this. - Get rid of the retry logic in accept(). I don't think it was needed at all. But I may be wrong.
1 parent dfad1a9 commit 11ba094

5 files changed

Lines changed: 89 additions & 247 deletions

File tree

Doc/lib/libsocket.tex

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ \section{\module{socket} ---
7272
related to socket or address semantics raise the error
7373
\exception{socket.error}.
7474

75-
Non-blocking mode is supported through the
76-
\method{setblocking()} method.
75+
Non-blocking mode is supported through
76+
\method{setblocking()}. A generalization of this based on timeouts
77+
is supported through \method{settimeout()}.
7778

7879
The module \module{socket} exports the following constants and functions:
7980

@@ -284,8 +285,7 @@ \section{\module{socket} ---
284285
descriptor is invalid. This function is rarely needed, but can be
285286
used to get or set socket options on a socket passed to a program as
286287
standard input or output (such as a server started by the \UNIX{} inet
287-
daemon). The socket is assumed to be in blocking mode without
288-
a timeout.
288+
daemon). The socket is assumed to be in blocking mode.
289289
Availability: \UNIX.
290290
\end{funcdesc}
291291

@@ -514,38 +514,39 @@ \subsection{Socket Objects \label{socket-objects}}
514514
\method{send()} call can't immediately dispose of the data, a
515515
\exception{error} exception is raised; in blocking mode, the calls
516516
block until they can proceed.
517+
\code{s.setblocking(0)} is equivalent to \code{s.settimeout(0)};
518+
\code{s.setblocking(1)} is equivalent to \code{s.settimeout(None)}.
517519
\end{methoddesc}
518520

519521
\begin{methoddesc}[socket]{settimeout}{value}
520-
Set a timeout on blocking socket operations. Value can be a
521-
nonnegative float expressing seconds, or \code{None}. If a float is
522+
Set a timeout on blocking socket operations. The \var{value} argument
523+
can be a nonnegative float expressing seconds, or \code{None}.
524+
If a float is
522525
given, subsequent socket operations will raise an \exception{error}
523526
exception if the timeout period \var{value} has elapsed before the
524527
operation has completed. Setting a timeout of \code{None} disables
525528
timeouts on socket operations.
529+
\code{s.settimeout(0.0)} is equivalent to \code{s.blocking(0)};
530+
\code{s.settimeout(None)} is equivalent to \code{s.setblocking(1)}.
526531
\versionadded{2.3}
527532
\end{methoddesc}
528533

529534
\begin{methoddesc}[socket]{gettimeout}{}
530535
Returns the timeout in floating seconds associated with socket
531-
operations, or \code{None} if no timeout is set.
536+
operations, or \code{None} if no timeout is set. This reflects
537+
the last call to \method{setblocking()} or \method{settimeout()}.
532538
\versionadded{2.3}
533539
\end{methoddesc}
534540

535-
Some notes on the interaction between socket blocking and timeouts: A
536-
socket object can be in one of three modes: blocking, non-blocking, or
537-
timout. Sockets are always created in blocking mode. In blocking
538-
mode, operations block until complete. In non-blocking mode,
539-
operations fail (with an error that is unfortunately system-dependent)
540-
if they cannot be completed immediately. In timeout mode, operations
541-
fail if they cannot be completed within the timeout specified for the
542-
socket.
543-
544-
Calling \method{settimeout()} cancels non-blocking mode as set by
545-
\method{setblocking()}; calling \method{setblocking()} cancels a
546-
previously set timeout. Setting the timeout to zero acts similarly
547-
but is implemented different than setting the socket in non-blocking
548-
mode (this could be considered a bug and may even be fixed).
541+
Some notes on socket blocking and timeouts: A socket object can be in
542+
one of three modes: blocking, non-blocking, or timout. Sockets are
543+
always created in blocking mode. In blocking mode, operations block
544+
until complete. In non-blocking mode, operations fail (with an error
545+
that is unfortunately system-dependent) if they cannot be completed
546+
immediately. In timeout mode, operations fail if they cannot be
547+
completed within the timeout specified for the socket. The
548+
\method{setblocking()} method is simply a shorthand for certain
549+
\method{settimeout()} calls.
549550

550551
Timeout mode internally sets the socket in non-blocking mode. The
551552
blocking and timeout modes are shared between file descriptors and

Lib/test/test_socket.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def _setUp(self):
5151
self.queue = Queue.Queue(1)
5252

5353
# Do some munging to start the client test.
54-
test_method = getattr(self, '_' + self._TestCase__testMethodName)
54+
methodname = self.id()
55+
i = methodname.rfind('.')
56+
methodname = methodname[i+1:]
57+
test_method = getattr(self, '_' + methodname)
5558
self.client_thread = thread.start_new_thread(
5659
self.clientRun, (test_method,))
5760

Lib/test/test_timeout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ def testRangeCheck(self):
5959
self.assertRaises(ValueError, self.sock.settimeout, -1L)
6060
self.assertRaises(ValueError, self.sock.settimeout, -1.0)
6161

62-
def testTimeoutThenoBlocking(self):
62+
def testTimeoutThenBlocking(self):
6363
"Test settimeout() followed by setblocking()"
6464
self.sock.settimeout(10)
6565
self.sock.setblocking(1)
6666
self.assertEqual(self.sock.gettimeout(), None)
6767
self.sock.setblocking(0)
68-
self.assertEqual(self.sock.gettimeout(), None)
68+
self.assertEqual(self.sock.gettimeout(), 0.0)
6969

7070
self.sock.settimeout(10)
7171
self.sock.setblocking(0)
72-
self.assertEqual(self.sock.gettimeout(), None)
72+
self.assertEqual(self.sock.gettimeout(), 0.0)
7373
self.sock.setblocking(1)
7474
self.assertEqual(self.sock.gettimeout(), None)
7575

0 commit comments

Comments
 (0)