【问题标题】:Messages received from port in erlang-sqlite3从 erlang-sqlite3 中的端口收到的消息
【发布时间】:2010-11-18 22:00:56
【问题描述】:

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.

因此,当取消注释 {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}
  1. 我哪里出错了?
  2. 我收到的关于端口错误的消息是否类似于{'EXIT',Port,Reason}

【问题讨论】:

    标签: erlang erlang-ports


    【解决方案1】:

    似乎在您的进程和端口之间涉及另一个进程,它对真实端口消息进行解码。你确定端口真的是端口吗?试试io:format("Port: ~p~n", [Port]) 如果你会看到#Port&lt;0.500&gt; 之类的东西,那就是端口,如果会是&lt;0.38.0&gt; 之类的东西,那么中间就是那个人。

    【讨论】:

    • 是的,刚刚检查过,确实是一个端口。
    • @Alexey:我不知道链接的驱动程序是否不能自己工作。
    【解决方案2】:

    http://www.erlang.org/doc/apps/erts/driver.html 中的相关示例是最后一个。原来在使用driver_output_term时,词条是自己发送的:

    receive
        Result ->
            Result
    end.
    

    而不是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 2014-02-18
      • 2023-03-31
      • 2011-05-07
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      相关资源
      最近更新 更多