Unexpected messages from a port driver

Alexey Romanov <>
Wed Oct 20 15:58:58 CEST 2010


[Duplicating my question on StackOverflow here.]

Erlang-sqlite3 uses a port driver to connect with the SQLite database,
and receives messages from the port in this way
(http://github.com/alexeyr/erlang-sqlite3/blob/56bfbe484d0c5b97d7b1f08677def3cc388787be/src/sqlite3.erl#L609):

    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.

According to the manual
(http://www.erlang.org/doc/reference_manual/ports.html), the following
messages can be received from a port:

    {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. Am I missing something obvious, or is message format given in the
manual wrong?
2. In the second case, what messages actually can be received from the
port? In particular, on error is the message `{'EXIT',Port,Reason}` or
something else?


Yours, Alexey Romanov


More information about the erlang-questions mailing list