从 erlang-sqlite3 端口接收的消息
Erlang-sqlite3 使用端口驱动程序连接 SQLite 数据库,并且 从端口接收消息:
wait_result(Port) ->
receive
{Port, Reply} ->
% io:format("Reply: ~p~n", [Reply]),
Reply;
{error, Reason} ->
io:format("Error: ~p~n", [Reason]),
{error, Reason};
_Else ->
io:format("Else: ~p~n", [_Else]),
_Else
end.
我认为来自端口的消息应该类似于 this:
{Port,{data,Data}} Data is received from the external program.
{Port,closed} Reply to Port ! {Pid,close}.
{Port,connected} Reply to Port ! {Pid,{connect,NewPid}}
{'EXIT',Port,Reason} If the port has terminated for some reason.
因此,当取消注释 {Port, Reply}
子句中的 io:format
行时,我应该会看到 {data, .. .}
获取实际回复。我不;相反,我看到(对于 test.erl
)
Reply: {ok,101}
Reply: [{columns,["name"]},{rows,[{<<"user">>}]}]
Reply: [{columns,["sql"]},
{rows,[{<<"CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, wage INTEGER)">>}]}]
Reply: {id,1}
Reply: {id,2}
Reply: [{columns,["id","name","age","wage"]},
{rows,[{1,<<"abby">>,20,2000},{2,<<"marge">>,30,2000}]}]
Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
Reply: [{columns,["id","name","age","wage"]},
{rows,[{1,<<"abby">>,20,2000},{2,<<"marge">>,30,2000}]}]
Reply: {ok,101}
Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
Reply: {ok,101}
- 我哪里出错了?
- 我收到的端口错误消息是否类似于
{'EXIT',Port,Reason}
?
Erlang-sqlite3 uses a port driver to connect with the SQLite database, and receives messages from the port:
wait_result(Port) ->
receive
{Port, Reply} ->
% io:format("Reply: ~p~n", [Reply]),
Reply;
{error, Reason} ->
io:format("Error: ~p~n", [Reason]),
{error, Reason};
_Else ->
io:format("Else: ~p~n", [_Else]),
_Else
end.
I thought that messages from ports should look like this:
{Port,{data,Data}} Data is received from the external program.
{Port,closed} Reply to Port ! {Pid,close}.
{Port,connected} Reply to Port ! {Pid,{connect,NewPid}}
{'EXIT',Port,Reason} If the port has terminated for some reason.
So, when uncommenting the io:format
line in {Port, Reply}
clause, I should expect to see {data, ...}
for actual replies. I don't; instead I see (for test.erl
)
Reply: {ok,101}
Reply: [{columns,["name"]},{rows,[{<<"user">>}]}]
Reply: [{columns,["sql"]},
{rows,[{<<"CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, wage INTEGER)">>}]}]
Reply: {id,1}
Reply: {id,2}
Reply: [{columns,["id","name","age","wage"]},
{rows,[{1,<<"abby">>,20,2000},{2,<<"marge">>,30,2000}]}]
Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
Reply: [{columns,["id","name","age","wage"]},
{rows,[{1,<<"abby">>,20,2000},{2,<<"marge">>,30,2000}]}]
Reply: {ok,101}
Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
Reply: {ok,101}
- Where am I going wrong?
- Will messages I get on a port error look like
{'EXIT',Port,Reason}
or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您的进程和端口之间涉及另一个进程,它负责解码真实端口消息。你确定那个港口真的是港口吗?尝试
io:format("Port: ~p~n", [Port])
如果您会看到类似#Port<0.500>
的内容,则它是端口,如果它类似于<0.38.0>
中间有一个人。It seems that between your process and port is another process involved which decodes real port messages. Are you sure that Port is really Port?. Try
io:format("Port: ~p~n", [Port])
If you will see something like#Port<0.500>
it is port, if it will be something like<0.38.0>
there is the man in the middle.http://www.erlang.org/doc/apps/erts 中的相关示例/driver.html 是最后一个。事实证明,当使用
driver_output_term
时,该术语是由其自身发送的:而不是
The relevant example in http://www.erlang.org/doc/apps/erts/driver.html is the last one. It turns out that when using
driver_output_term
, the term is sent by itself:instead of