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

Evan Miller <>
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/'"}, []).
#Port<0.104>
23> Port ! {self(), {command, "foo\n"}}.
{<0.56.0>,{command,"foo\n"}}
24> flush(). % still waiting for output...
ok
25> Port ! {self(), close}.
{<0.56.0>,close}
26> sed: couldn't flush stdout: Broken pipe

26>

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

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

Is anything like this possible?

Thanks,

Evan Miller




More information about the erlang-questions mailing list