从 erlang-sqlite3 端口接收的消息

发布于 2024-09-28 02:53:46 字数 1883 浏览 7 评论 0原文

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}
  1. 我哪里出错了?
  2. 我收到的端口错误消息是否类似于 {'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}
  1. Where am I going wrong?
  2. Will messages I get on a port error look like {'EXIT',Port,Reason} or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-10-05 02:53:46

看来您的进程和端口之间涉及另一个进程,它负责解码真实端口消息。你确定那个港口真的是港口吗?尝试 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.

一片旧的回忆 2024-10-05 02:53:46

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

receive
    Result ->
        Result
end.

而不是

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

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:

receive
    Result ->
        Result
end.

instead of

receive
    {Port, {data, Result}} ->
        Result
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文