gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:06:44 25 4
gpt4 key购买 nike

当我在下面的示例中调用没有 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

从文档中我不明白行为的差异。

最佳答案

当您在 cat >bar 中重定向文件中的输出时,命令 shell 会关闭 stdout 并且 Erlang 在这种情况下只会关闭端口,因为端口默认情况下会尝试使用命令输出并关闭 eof。修复它的正确方法是使用 out 选项,正如 butter71 已经建议的那样。 outexit_statuserror_to_stdout 等选项告诉端口不要打扰 stdout

关于erlang - 未使用 `exit_status` 选项时,open_port 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com