Erlang端口:与“wc”类程序接口? [英] Erlang Ports: Interfacing with a "wc"-like program?

查看:155
本文介绍了Erlang端口:与“wc”类程序接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部exe程序,从stdin读取并产生结果。它的工作方式与 wc 程序一样,直到EOF。 (或流结束,而不是。)



更新:让我再补充一点解释:我基本上是试图写一个Erlang pipe。



我可以在批处理文件中调用程序,如 echo 339371249625 | LookupProj.exe 但是我希望能够从Erlang gen_server 中传递数据。


$ b $我看过Erlang Ports,但是我很难让他们玩得很好。这是我有的:

  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 - 如何关闭cat进程的标签?
接收B - > io:format(〜p,[B])end。

我尝试使用 eof open_port 中没有帮助。 (不知道这是否是正确的标志?)



我在哪里出错?谢谢!

解决方案

如果我理解正确,您正尝试重复使用多个呼叫之间的端口连接,如 echo 339371249625 | LookupProj.exe ,但afaik关闭 stdin 的唯一方法实际上是用 port_close / 1 ,所以所有这些围绕端口跳舞的事情都不如使用 os:cmd / 1



如果您可以修改LookupProj.exe,您可以在stdin中考虑一些预定义的字节序列作为命令的结尾,并且每次发送时都发送完成而不是EOF。


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!

解决方案

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.

这篇关于Erlang端口:与“wc”类程序接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆