2

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.

我认为来自端口的消息应该如下所示

{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, ...}实际的回复。我不; 相反,我看到 (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}
  1. 我哪里错了?
  2. 我收到的有关端口错误的消息是否看起来像{'EXIT',Port,Reason}
4

2 回答 2

0

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

receive
    Result ->
        Result
end.

代替

receive
    {Port, {data, Result}} ->
        Result
end.
于 2010-11-18T14:00:56.390 回答
0

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

于 2010-10-21T13:08:26.497 回答