[erlang-questions] open_pipe({spawn, "cat"}) --- how to close stdin of cat?

C K Tan cktan@REDACTED
Sun Mar 1 11:48:36 CET 2009


Michael,

Without closing stdin of wc, wc never returns because it thought there
are more data coming.

wc(InputText) ->
   P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
                           stderr_to_stdout, in, out, eof]),
   port_command(P, InputText),
   %% P ! {self(), {eof}},   %% ERROR -- how to close stdin of the wc process?
   receive {P, X} -> X end,
   port_close(P).


wc("hello world").  %% blocks forever.




On Sun, Mar 1, 2009 at 2:18 AM, Michael Truog <mjtruog@REDACTED> wrote:
> You normally never try to close stdin on the port.  You can close stdin on
> the port by calling erlang:port_close/1 but you won't be able to receive on
> the port after you close it.  You should be able to use a read within the
> port program that does not depend on eof to terminate the data (see
> http://erlang.org/doc/tutorial/part_frame.html by the Ports section).
>
> The documentation for erlang:open_port/2 is at
> http://erlang.org/doc/man/erlang.html .  The system commands without a
> module name are within the erlang module.  Consider using
> erlang:port_command/2 rather than using ! directly, for better behavior.
>
> - Michael
>
> C K Tan wrote:
>>
>> Hi all,
>>
>> I am tryinng to use port to open a program that reads until EOF from
>> stdin, and then write results to stdout ... i.e. something like the
>> unix wc command. I can't figure out how to close the stdin of the
>> spawned process:
>>
>> wc(InputText) ->
>>    P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
>>                            stderr_to_stdout, in, out, eof]),
>>    P ! {self(), {command, "hello world"}},
>>    P ! {self(), {eof}},   %% ERROR -- how to close stdin of the cat
>> process?
>>    receive {P, X} -> X end.
>>
>> Also, where can I find detailed documentation on open_port?
>>
>> Appreciate any help.
>>
>> Thanks,
>> -cktan
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
>



More information about the erlang-questions mailing list