Created at:
Modified at:
Erlang notes
(2018-04-22)
General tips
In this section I just want to document stuff for people that, like me, are beginners. Although there is very simple stuff for someone who has already some experience with Erlang, I think it is worth to document and it may be helpful for those who are starting to learn the language.
I'm also learning the language! Tips below probably have wrong advices so, if you see something strange, please, let me known.
Node communication
I was starting a multi process application with two nodes, say node_a
and
node_b
, and I was having problems to understand how to make both to get
connected. First, when executing a node with erl, you have to pass the
-sname
flag (or the -name
flag, but with a different syntax) with the
node name. Let's first both files contents:
sendmsg.erl
:
-module(sendmsg).
-export([main/0]).
main() ->
register(sendmsg, self()),
{receivemsg, 'node_receive@tifa'} ! "a message".
receivemsg.erl
::
-module(receivemsg).
-export([main/0]).
main() ->
register(receivemsg, self()),
loop().
loop() ->
receive
Msg -> io:format("~p~n", [Msg])
end,
loop().
Now, how you execute it? Run::
$ erlc receivemsg.erl&& erl -noshell -sname node_receive -run receivemsg main
And in another different shell, run::
$ erlc sendmsg.erl && erl -noshell -sname node_send -run sendmsg main
Note we are executing the receive node first.
The important option we pass to erl
is -sname
, for the name of the
node. When passing the message from one process to another, in a different
node, we need to use the syntax {Process, Node} ! Msg
. The Node
contents name should be the same we passed as argument to the -sname
option.
Unless you know the Pid of the Process, it might be a good idea to register it so you can use atoms to refer to it.
Note that the sending process should also be run in a known node, i.e., it
should also have the -sname
option, otherwise it will not be able to send
message to the receiving process.
Troubleshooting
Error with Mnesia: "no function clause matching ..."
(2010-09-04)
If you get a crypt error like that::
4> test:vai().
** exception error: no function clause matching
test:'-customer_all/0-lc$^0/1-0-'({qlc_handle,
{qlc_table,#Fun<mnesia.20.112329951>,true,
#Fun<mnesia.21.62211129>,
#Fun<mnesia.22.75429001>,
#Fun<mnesia.23.26336897>,
#Fun<mnesia.26.62819299>,
#Fun<mnesia.25.51075493>,
#Fun<mnesia.24.47804912>,'=:=',undefined,
no_match_spec}})
in function test:customer_all/0
Check if you insert the following line on the top of your file::
-include_lib("stdlib/include/qlc.hrl").
The inclusion of the qlc.hrl
header is necessary if you want to use Mnesia
in your module. Got this tip from the following message:
Re: [Erlide-devel] build path to include stdlib/qlc
net_kernel:connect_node/1
timeout
(2018-12-14)
net_kernel:connect_node/1
is not very verbose about what is happening. If
you got a timeout problem, check if the host you are referring is resolvable.
In my case I got an entry pointing to wrong IP address in /etc/hosts
.