[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
numbers to express fractions of a second), a zero value to mean simply poll and
return immediately, or omitted to mean wait until at least one object is ready (as
done in our server script). The call returns a triple of ready objects—subsets of the
first three arguments—any or all of which may be empty if the timeout expired
before sources became ready.

select portability
Like threading, but unlike forking, this server works in standard Windows Python,
too. Technically, the select call works only for sockets on Windows, but also
works for things like files and pipes on Unix and Macintosh. For servers running
over the Internet, of course, the primary devices we are interested in are sockets.


Nonblocking sockets
select lets us be sure that socket calls like accept and recv won’t block (pause) the
caller, but it’s also possible to make Python sockets nonblocking in general. Call
the setblocking method of socket objects to set the socket to blocking or non-
blocking mode. For example, given a call like sock.setblocking(flag), the socket
sock is set to nonblocking mode if the flag is zero and to blocking mode otherwise.
All sockets start out in blocking mode initially, so socket calls may always make
the caller wait.
However, when in nonblocking mode, a socket.error exception is raised if a
recv socket call doesn’t find any data, or if a send call can’t immediately transfer
data. A script can catch this exception to determine whether the socket is ready for
processing. In blocking mode, these calls always block until they can proceed. Of
course, there may be much more to processing client requests than data transfers
(requests may also require long-running computations), so nonblocking sockets
don’t guarantee that servers won’t stall in general. They are simply another way to
code multiplexing servers. Like select, they are better suited when client requests
can be serviced quickly.


The asyncore module framework
If you’re interested in using select, you will probably also be interested in checking
out the asyncore.py module in the standard Python library. It implements a class-
based callback model, where input and output callbacks are dispatched to class
methods by a precoded select event loop. As such, it allows servers to be con-
structed without threads or forks, and it is a select-based alternative to the sock
etserver module’s threading and forking module we met in the prior sections. As
for this type of server in general, asyncore is best when transactions are short—
what it describes as “I/O bound” instead of “CPU bound” programs, the latter of
which still require threads or forks. See the Python library manual for details and
a usage example.


Twisted
For other server options, see also the open source Twisted system (http://twisted
matrix.com). Twisted is an asynchronous networking framework written in Python
that supports TCP, UDP, multicast, SSL/TLS, serial communication, and more. It


Handling Multiple Clients | 825
Free download pdf