Erlang Ports: Interfacing with a “wc”-like program?

我与影子孤独终老i 提交于 2019-11-30 18:00:33

问题


I have an external exe program that reads from the stdin and produces a result. It works like the wc program and reads until the EOF. (Or End of Stream, rather.)

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

I'm able to call the program in a batch file like echo 339371249625 | LookupProj.exe but I want to be able to pass data to this from an Erlang gen_server.

I've looked at Erlang Ports but I'm having trouble getting them to play nice. Here's 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've tried using the eof flag in open_port to no help. (Not sure if this is the right flag?)

Where did I go wrong? Thanks!


回答1:


If I understand correctly you are trying to re-use port connection between several calls like echo 339371249625 | LookupProj.exe, but afaik the only way to close the stdin is actually to close the port with port_close/1, so all this dance around ports is not any better than launching that commands with os:cmd/1.

If you can modify that LookupProj.exe you'd want to make consider some predefined byte sequence at stdin as an end of command and just send it each time you're done instead of EOF.



来源:https://stackoverflow.com/questions/8792376/erlang-ports-interfacing-with-a-wc-like-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!