[erlang-questions] Communicating processes
Peter Lund
erlang@REDACTED
Thu Mar 29 08:10:12 CEST 2007
Fredrik Hoback skrev:
> Hi, I'm having some trouble with inter-process communication. First of
> all I have a process which receives a message it looks something like
> this:
>
> %%%%%%%%%%%%%%%%%%%%%%%%
> receive_msg() ->
> receive
> {Msg} ->
> io:format("Recieved Message");
> _->
> io:format("Nothing to report")
> end.
> %%%%%%%%%%%%%%%%%%%%%%%%
> Note: this process is spawned and registered as proxy.
>
> To continue: I have another module which I start from within the shell
> it looks similar to this:
>
> %%%%%%%%%%%%%%%%%%%%%%%%
> send_message()->
> Msg = "Hello World", % My message
> send(Msg), % here I send my message somewhere unknown and the reply is
> sent to the process proxy above
> %Here I want to receive the message that is received in proxy
> %%%%%%%%%%%%%%%%%%%%%%%%
>
> So how to I accomplish this? Is there any other way then using " ! "?
> What I would like to do is to call the process proxy from within
> send_message and the return answer should be Msg, is this possible?
> And how should it look?
I am not sure what you are looking for, but anyhow if you would like to
implement a function "call" to retrieve something from another process:
%% process A
...
NameOfB = call(BPid, what_is_your_name)
...
call(Pid, Msg) ->
Ref = make_ref(),
Pid ! {call_request, Ref, self(), Msg},
receive
{call_response, Ref, Resp} -> Resp
after 5000 -> timeout
end.
%% process B
loop() ->
receive {call_request, Ref, From, what_is_your_name} ->
From ! {call_response, Ref, "Barbados"}
end,
loop().
The make_ref() returns a unique refernce assuring the caller that it is
the response to the correct request. Observer that the call here is
allowed to timeout after 5000 millisecs.
More information about the erlang-questions
mailing list