Ports Erlang: Interaction with the wc program?

I have an external exe program that reads from stdin and produces the result. It works as a wc program and reads before EOF. (Or the end of the stream, rather.)

Update: let me add one more explanation: I'm basically trying to write an Erlang pipe.

I can call the program in a batch file, for example echo 339371249625 | LookupProj.exe echo 339371249625 | LookupProj.exe , but I want to be able to transfer data from this file from Erlang gen_server .

I looked at the ports of Erlang, but I have problems making them play well. Here is what I have:

 test(InputText) -> P = open_port({spawn, "/ExternEvent/LookupProj.exe"}, [stream, exit_status, use_stdio, stderr_to_stdout, in, out]), IBin = list_to_binary(InputText), %% io:format("~p~n",[I2]), P ! {self(), {command, <<IBin/binary, <<26>>/binary>>}}, %% ASCII 26 = EOF P ! {self(), {eof}}, %% ERROR -- how to close stdin of the cat process? receive B -> io:format("~p",[B]) end. 

I tried using the eof flag in open_port without help. (Not sure if this is the correct flag?)

Where am I wrong? Thanks!

+4
source share
1 answer

If I understand correctly, you are trying to reuse a port connection between multiple calls, for example echo 339371249625 | LookupProj.exe , but afaik the only way to close stdin is to actually close the port using port_close / 1 , so all this dance around the ports is no better than running commands with os: cmd / 1 .

If you can change this LookupProj.exe, you would like to consider some predefined byte sequence on stdin as the end of the command and just send it every time you finish, not EOF.

+1
source

Source: https://habr.com/ru/post/1390089/


All Articles