不使用`exit_status`选项时,open_port不起作用

时间:2011-07-01 15:50:43

标签: erlang

当我在下面的示例中调用不带exit_status的open_port时,它无法使用:

Eshell V5.7.5  (abort with ^G)
1>  P = open_port({spawn, "cat >bar"}, [stream, use_stdio]).
#Port<0.498>
2> port_command(P, "hello\n").
** exception error: bad argument
     in function  port_command/2
        called as port_command(#Port<0.498>,"hello\n")

但是,当我只添加exit_status并保持一切相同时,它就会起作用:

Eshell V5.7.5  (abort with ^G)
1> P = open_port({spawn, "cat >bar"}, [stream, use_stdio, exit_status]).
#Port<0.498>
2> port_command(P, "hello\n").
true

从文档中我不了解行为上的差异。

2 个答案:

答案 0 :(得分:5)

当您在cat >bar命令中重定向文件时,命令shell关闭stdout并且在这种情况下Erlang只关闭端口,因为端口尝试默认使用命令输出并在eof关闭。解决问题的正确方法是使用out选项作为已建议的 butter71 outexit_statuserror_to_stdout等选项告诉端口不要担心stdout

答案 1 :(得分:4)

尝试仅为输出设置端口:

P = open_port({spawn, "cat >bar"}, [stream, use_stdio, out]).
port_command(P, "hello\n").

我的猜测是,因为你重定向了命令的标准输出,所以erlang不满意。