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

(yzsuai) #1

already seen in this and the prior chapters. For example, the following simple mecha-
nisms can all be interpreted as cross-program communication devices:



  • Simple files

  • Command-line arguments

  • Program exit status codes

  • Shell environment variables

  • Standard stream redirections

  • Stream pipes managed by os.popen and subprocess


For instance, sending command-line options and writing to input streams lets us pass
in program execution parameters; reading program output streams and exit codes gives
us a way to grab a result. Because shell environment variable settings are inherited by
spawned programs, they provide another way to pass context in. And pipes made by
os.popen or subprocess allow even more dynamic communication. Data can be sent
between programs at arbitrary times, not only at program start and exit.


Beyond this set, there are other tools in the Python library for performing Inter-Process
Communication (IPC). This includes sockets, shared memory, signals, anonymous and
named pipes, and more. Some vary in portability, and all vary in complexity and utility.
For instance:



  • Signals allow programs to send simple notification events to other programs.

  • Anonymous pipes allow threads and related processes that share file descriptors to
    pass data, but generally rely on the Unix-like forking model for processes, which
    is not universally portable.

  • Named pipes are mapped to the system’s filesystem—they allow completely unre-
    lated programs to converse, but are not available in Python on all platforms.

  • Sockets map to system-wide port numbers—they similarly let us transfer data be-
    tween arbitrary programs running on the same computer, but also between pro-
    grams located on remote networked machines, and offer a more portable option.


While some of these can be used as communication devices by threads, too, their full
power becomes more evident when leveraged by separate processes which do not share
memory at large.


In this section, we explore directly managed pipes (both anonymous and named), as
well as signals. We also take a first look at sockets here, but largely as a preview; sockets
can be used for IPC on a single machine, but because the larger socket story also involves
their role in networking, we’ll save most of their details until the Internet part of this
book.


Other IPC tools are available to Python programmers (e.g., shared memory as provided
by the mmap module) but are not covered here for lack of space; search the Python


Interprocess Communication | 223
Free download pdf