tags:

views:

330

answers:

3

Let's suppose that I have 2 processes in Erlang, and each process has a receive loop running. I want to send a signal from ProcessB to ProcessA but ProcessA doesn't actually need to do anything with it. ProcessA only needs to know that ProcessB sent the message.

Here's how I have it implemented currently:

receive   
    {message_from_process_b} ->  
        io:format("received a message from b", []);  
end,  
%% at this point I know that I've received the message from B.

It works fine. But out of curiosity, how can I write this without the io:format line? (I need Process A to block until receiving the message from B, this is part of a larger Yaws / Yapp and the server needs a response before it can show the page.)

+6  A: 

you could replace the io:format with an atom like nop

Simeon Pilgrim
+5  A: 

You really should be building an OTP application and Process B should be a gen_server.

The message sending semantics and building your own send/receive protocols are all very fine but unless you really know what you are doing you will start building unmaintainable servers.

My advice would be sit down and work out how to make your application a properly structured standard OTP application so that you start Yaws as part of an application tree and do all the other stuff with ProcessA and ProcessB inside the normal OTP framework.

Gordon Guthrie
Thanks. I plan on it. I'm still learning Erlang, and even Joe's book mentions that he writes code and then puts all of the OTP stuff around it. I'm taking that approach to see what happens. I understand gen_server and supervisors, but have yet to implement one from scratch.Definitely - thank you for the advice.
marcc
Gordon Guthrie
+2  A: 

As Simeon Pilgrim said, you can do

receive   
    {message_from_process_b} -> ok
end,

But you might want to add a timeout:

receive   
    {message_from_process_b} -> ok
    after 1000 -> io:format("timeout!", [])
end,

to cater for never receiving the message (i.e. a fault occurring in the sensder).

bjnortier
In that case it should probably throw an exception, otherwise it is a silent failure. Crash early!
Christian