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

(yzsuai) #1

  • Arbitrary Python objects like lists and dictionaries (or at least copies of them) can
    be transferred over sockets, too, by shipping the serialized byte strings produced
    by Python’s pickle module introduced in Chapter 1 and covered in full in
    Chapter 17.

  • As we’ll see in Chapter 10, the printed output of a simple script can be redirected
    to a GUI window, by connecting the script’s output stream to a socket on which
    a GUI is listening in nonblocking mode.

  • Programs that fetch arbitrary text off the Web might read it as byte strings over
    sockets, but manually decode it using encoding names embedded in content-type
    headers or tags in the data itself.

  • In fact, the entire Internet can be seen as a socket use case—as we’ll see in Chap-
    ter 12, at the bottom, email, FTP, and web pages are largely just formatted byte
    string messages shipped over sockets.


Plus any other context in which programs exchange data—sockets are a general, port-
able, and flexible tool. For instance, they would provide the same utility as fifos for the
GUI/debugger example used earlier, but would also work in Python on Windows and
would even allow the GUI to connect to a debugger running on a different computer
altogether. As such, they are seen by many as a more powerful IPC tool.


Again, you should consider this section just a preview; because the grander socket story
also entails networking concepts, we’ll defer a more in-depth look at the socket API
until Chapter 12. We’ll also see sockets again briefly in Chapter 10 in the GUI stream
redirection use case listed above, and we’ll explore a variety of additional socket use
cases in the Internet part of this book. In Part IV, for instance, we’ll use sockets to
transfer entire files and write more robust socket servers that spawn threads or processes
to converse with clients to avoid denying connections. For the purposes of this chapter,
let’s move on to one last traditional IPC tool—the signal.


Signals


For lack of a better analogy, signals are a way to poke a stick at a process. Programs
generate signals to trigger a handler for that signal in another process. The operating
system pokes, too—some signals are generated on unusual system events and may kill
the program if not handled. If this sounds a little like raising exceptions in Python, it
should; signals are software-generated events and the cross-process analog of excep-
tions. Unlike exceptions, though, signals are identified by number, are not stacked, and
are really an asynchronous event mechanism outside the scope of the Python interpreter
controlled by the operating system.


In order to make signals available to scripts, Python provides a signal module that
allows Python programs to register Python functions as handlers for signal events. This
module is available on both Unix-like platforms and Windows (though the Windows
version may define fewer kinds of signals to be caught). To illustrate the basic signal


240 | Chapter 5: Parallel System Tools

Free download pdf