[erlang-questions] pattern matching registered name

James Aimonetti james@REDACTED
Mon May 20 21:34:52 CEST 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/20/2013 11:47 AM, Edmund Sumbar wrote:
> On Mon, May 20, 2013 at 11:47 AM, James Aimonetti
> <james@REDACTED> wrote:
> 
>> rpc/2 expects the first arg into the function to be the first
>> element in the response tuple. The loop/0 uses self(), which
>> always returns a pid(), not the registered name. So you call rpc
>> with an atom, and receive a tuple with a pid(). Those don't
>> match. Your call to rpc/2 hangs because the selective receive in
>> rpc/2 never matches (and you have no timeout clause).
>> 
> 
> What's confusing is that the atom apparently resolves into a pid()
> in the send expression of kvsx:rpc/2 (adding io:format("reg name =
> ~p~n", [whereis(abc)]) to the function returns the pid()), but not
> in the pattern tuple of the receive expression.
> 
> Anyway, thanks for the replies James and Yogish.
> 
> Ed
> 
> 
> 
> _______________________________________________ erlang-questions
> mailing list erlang-questions@REDACTED 
> http://erlang.org/mailman/listinfo/erlang-questions
> 

You could change rpc/2:

rpc(Name, Q) when is_atom(Name) ->
  rpc(whereis(Name), Q);
rpc(Pid, Q) when is_pid(Pid) ->
  Pid ! {self(), Q},
  receive
    {Pid, Reply} ->
      Reply
  end.

This will crash the caller if Name isn't registered, FYI.

- -- 
James Aimonetti
Distributed Systems Engineer / DJ MC_

2600hz | http://2600hz.com
sip:james@REDACTED
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJRmnrcAAoJENc77s1OYoGgF1EH/Ajv0qTcrUEK+NL4kNJ7w8rQ
G6hdnnpm6231XA6h9MbmW0jph5nYdAo5PBeF0QBK3XyuVOUw1AvZv8PcqTTHmsKc
K0RbZFip7mfNWXFa3wPufg1K7cZ8F5yG4k5BB+PpHSRfnh+UVs7z+qVNeXKXBWp2
OClbohj6BqzuRR8udb+k73Bz1uwsIqDDWTSEtBpJ4YQJwSmTBjlHKIJ6wnEi5yl3
VXFdM+/KKBN0coO3TOWOvTt4soN2AqLKSKL7XfNs91FyJvAL/nJ1YderAY2Q9MY8
MZCLxF/Nsjjm4OxlabTEyMP/wnKwmNNvKmjx7Adx0ve+SoZbjayD32zl5goIQU8=
=IwqX
-----END PGP SIGNATURE-----



More information about the erlang-questions mailing list