[erlang-questions] Newbie: half-closing a port?

Evan Miller emmiller@REDACTED
Sat Feb 2 22:40:01 CET 2008

After poring over the list archives for a while, I cannot find a way to 
do the following fairly simple operation:

1. Open a port
2. Send data to the port
3. Close the writing file descriptor
4. Read data from the port
5. Close the port

My external program is a standard Unix utility that will not print data 
until it has read to the end of the input (i.e. the writer has closed 
its FD). If I close the port entirely, the external program gets a 
SIGPIPE when it attempts to flush its buffers. The following shell 
session demonstrates my problem:

22> Port = open_port({spawn, "sed 's/foo/bar/'"}, []).
23> Port ! {self(), {command, "foo\n"}}.
24> flush(). % still waiting for output...
25> Port ! {self(), close}.
26> sed: couldn't flush stdout: Broken pipe


I would like to be able to do something like this just before closing:

Port ! {self(), eof}.
% read output...

Is anything like this possible?


Evan Miller

More information about the erlang-questions mailing list