Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Each process just return it's own pid when performing lookup_pids #132

Open
hamdijmii1992 opened this issue Jan 29, 2017 · 2 comments
Open

Comments

@hamdijmii1992
Copy link

hamdijmii1992 commented Jan 29, 2017

I tried to regsiter some processes with a famliy name with gproc.
For this reason I created a gen_server that contain two function, the first one is to
handle registration and the second one is to lookup Pids of registered processes.
After that, I opnened two erlang consoles and I registered two processes with the same property
(each console request the server to register one process)
My server code is as follows:

`start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [],[]).

init([]) -> gproc:start_link(), {ok, []}.
%% Synchronous call
register(Pid, Name) ->
gen_server:call(Pid, {register, Name}).

getpids(Pid, Name) ->
gen_server:call(Pid, {getpids, Name}).

handle_call({register, Name}, _From, State) ->
gproc:reg_or_locate({p,l,Name}),
{reply, Name, State};

handle_call({getpids, Name}, _From, State) ->
Pids = gproc:lookup_pids({p,l,Name}),
{reply, Pids, State}.

handle_info(Msg, State) ->
io:format("Unexpected message: pn",[Msg]),
{noreply, State}.

terminate(normal, State) ->
ok.`

I registered my processes with
server_name:register(PID,<<"test">>)
and I lookup the pids with:
server_name:getpids(PID,<<"test">>)

But when I tried to get the pids of my family processes (Basically I have to pull a list
with 2 pids) I got just one pid (each console just lookup the pid registered by him self
and not render the pid registered with the other console).

Thanks for your help.
Best Regards.

@hamdijmii1992 hamdijmii1992 changed the title Each process just return it's pid when performing lookup_pids Each process just return it's own pid when performing lookup_pids Jan 29, 2017
@tahteche
Copy link

tahteche commented Aug 14, 2017

Have not tested your code yet but from what I see you are registering locally and doing lookups locally because of the l in gproc:reg_or_locate({p,l,Name}), and Pids = gproc:lookup_pids({p,l,Name}),. This means your properties are registered just within each node (the two consoles in your case I guess).

Try doing a global registration and lookup using g in place of l that is {p,l,Name} should be {p,g,Name}

@uwiger
Copy link
Owner

uwiger commented Aug 14, 2017

The function gproc:reg_or_locate/1 is only intended for unique names, and should crash if called with a (non-unique) property:

Eshell V7.3.1  (abort with ^G)
1> application:ensure_started(gproc).
ok
2> gproc:reg_or_locate({p,l,foo}).
** exception error: bad argument
     in function  gproc:reg_or_locate/1
        called as gproc:reg_or_locate({p,l,foo})

If you know that you'll only register a name once, you can use gproc:reg/1. If you want the call to be tolerate repeated registrations of the same name, gproc:ensure_reg/1 works nicely:

3> gproc:ensure_reg({p,l,foo}).
new
4> gproc:ensure_reg({p,l,foo}).
updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants