从erlang-sqlite3中的端口收到的消息

时间:2010-10-20 08:59:28

标签: erlang erlang-ports

Erlang-sqlite3使用端口驱动程序连接SQLite数据库,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.

我认为来自端口的邮件应该看起来像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.

因此,当取消注释io:format子句中的{Port, Reply}行时,我应该会看到{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}
  1. 我哪里错了?
  2. 我收到的端口错误信息是否会显示为{'EXIT',Port,Reason}

2 个答案:

答案 0 :(得分:0)

似乎您的进程和端口之间是另一个涉及解码实际端口消息的进程。你确定港口真的是港口吗?试试io:format("Port: ~p~n", [Port])如果你会看到类似#Port<0.500>的内容,那就是端口,如果它会像<0.38.0>那样,那么中间就有人。

答案 1 :(得分:0)

http://www.erlang.org/doc/apps/erts/driver.html中的相关示例是最后一个。事实证明,当使用driver_output_term时,该术语将自行发送:

receive
    Result ->
        Result
end.

而不是

receive
    {Port, {data, Result}} ->
        Result
end.