2012-01-09 34 views
4

我有一個外部exe程序從stdin讀取併產生結果。它的工作方式與wc程序相同,並讀取到EOF。 (或流的結束,而寧願)。Erlang Ports:與類似「wc」程序的接口?

更新:讓我再添加一個解釋:我基本上試圖編寫一個Erlang管道。

我可以在批處理文件中調用該程序,如echo 339371249625 | LookupProj.exe,但我希望能夠通過Erlang gen_server將數據傳遞給該程序。

我看過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 -- how to close stdin of the cat process? 
    receive B -> io:format("~p",[B]) end. 

我使用eof標誌試圖open_port沒有幫助。 (不知道這是否是正確的標誌?)

我哪裏錯了?謝謝!

回答

1

如果我理解正確,您正試圖重複使用幾個呼叫之間的端口連接,如echo 339371249625 | LookupProj.exe,但afaik關閉stdin的唯一方法實際上是關閉與port_close/1端口,所以這些圍繞端口跳舞並不比啓動該命令與os:cmd/1更好。

如果你可以修改LookupProj.exe你想要做考慮在標準輸入一些預定義的字節序列作爲命令的結束,只是每次你做,而不是EOF時間發送。