Monday, 28 September 2009

message passing

   1: -module(play).
   2: -export([headstrip/1,message/1,start/0,receiver/0,sender/1]).
   4: sender(RxPid) ->
   5:     RxPid ! {hey, self()}.
   7: receiver() ->
   8:     io:format("in receiver ~p~n",[self()]),     
   9:     receive
  10:         {hey, TxPid} -> io:format("rx: ~p ~p~n",[hey, TxPid])
  11:     end.
  13: start() ->
  14:     RxPid = spawn(play, receiver, []),
  15:     spawn(play, sender, [RxPid]).
28> play:start().
in receiver
rx: hey

receive blocks, and handles one message

If you want your receiver to hang around, call it again. e.g. add a call to receiver() just before end in the code above. But then – whole process finishes. Is something running in the background still? How do I check? [see]

remember to export

Remember that every function called must be exported – not just the bootstrap function you use to kick things off. In this context the error

=ERROR REPORT==== 25-Sep-2009::16:51:33 ===
Error in process <0.98.0> with exit value: {undef,[{play,sender,[<0.97.0>]}]}
indicates that you've forgotten to export the function "sender".

unhandlable messages are just dropped

Messages that receive doesn’t know how to handle are just forgotten. How to do a default response? Try sending eef instead of hey in the code above.

No comments: