One of the strongest reasons to consider Erlang for a programming project is concurrency. Erlang makes concurrency and distributed work dead simple.
If you have two nodes, nodex and nodey , and you want to be able to do something on both you can start the Erlang shell on nodex with the following:
erl -setcookie someFormOfPreSharedSecret -name nodex
and a similar command on nodey
erl -setcookie someFormOfPreSharedSecret -name nodey
The key here is to make sure that both nodes have the same magic cookie value, otherwise they won't be able to communicate.
From nodex issue
net_adm:ping('nodey@your_domain_here.com'). If the connection was successful you'll see the result pong, if something didn't go right, you'll see pang. From this point, you can run:
and see every node your currently connected with.
Now for the magic, let's say you want to start a particular task running on all of your nodes. We're going to achieve this by using the lists:foldl function:
http://gist.github.com/155217
Foldl takes 3 arguments: an anonymous function to call for every element of the third parameter, an accumulator to pass to the anonymous function, and the the list of things to iterate over. If your more familiar with Ruby this would hypothetically look like:
http://gist.github.com/155225
The key difference being Ruby doesn't return Pids to later refer back to those concurrent processes we started.
After running the Erlang example every node will run io:format and print a simple message. That's really all there is to distributing Erlang processing power. By retaining a list of the remote Pids you can do neat tricks with firing up a good number of initial processes and then sending them subsequent messages. I'll make a valiant effort to write about this more.
Permalink
| Leave a comment »