uwiger / gproc

Extended process registry for Erlang
Apache License 2.0
1.07k stars 232 forks source link

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

Open hamdijmii1992 opened 7 years ago

hamdijmii1992 commented 7 years ago

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.

tahteche commented 7 years ago

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 commented 7 years ago

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