Pipes
Pipes, another cross-program communication device, are made
available in Python with the built-in os.pipe
call. Pipes are unidirectional
channels that work something like a shared memory buffer, but with an
interface resembling a simple file on each of two ends. In typical
use, one program writes data on one end of the pipe, and another reads
that data on the other end. Each program sees only its end of the
pipes and processes it using normal Python file calls.
Pipes are much more within the operating system, though. For instance, calls to read a pipe will normally block the caller until data becomes available (i.e., is sent by the program on the other end) instead of returning an end-of-file indicator. Because of such properties, pipes are also a way to synchronize the execution of independent programs.
Anonymous Pipe Basics
Pipes come in two flavors—anonymous and named. Named pipes (sometimes called fifos) are represented by a file on your computer. Anonymous pipes exist only within processes, though, and are typically used in conjunction with process forks as a way to link parent and spawned child processes within an application; parent and child converse over shared pipe file descriptors. Because named pipes are really external files, the communicating processes need not be related at all (in fact, they can be independently started programs).
Since they are more traditional, let’s start with a look at
anonymous pipes. To illustrate, the script in Example 5-16 uses the os.fork ...
Get Programming Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.