[erlang-questions] remote spawns and sending messages to the remote PID

Bernard Duggan <>
Tue Apr 14 04:05:15 CEST 2009


Hi Jeff,
    The problem's pretty straightforward.
This:
> Now I spawn the counter on the bar node:
> ()3> S=spawn('',counter,run,[5]).
> <5734.41.0>
>   
Returns the process ID of the process executing the counter:run function.

Whereas this:
> ()5> T=counter:run(5).
> <0.48.0>
>   
Returns the return value of the counter:run function, which is a process
spawned from within that function.

Basically, you're getting confused between the return value of spawn(),
which is a function that creates a process, and the return value of
counter:run which is also a function that creates a process.  When you
call spawn('somenode', counter, run, [5]) it launches a process which
executes counter:run, which in turn launches another process (by virtue
of having a further call to spawn() in it).  It's the process returned
by the spawn() /inside/ counter:run() that you want to send the message
to.  Try this instead:

S = spawn('', counter, counter, [5, 0]).

That will bypass the extra call to spawn() and give you the PID you
actually want to send messages to.

Cheers,

B



More information about the erlang-questions mailing list