问题
I'm using
spawn (node, module, function, Args)
global:register_name(name, pid)
To register a process on a different node globally.
Here's the code
Pid = spawn(mi, loop, [X]),
io:format("Glavni PID: ~w~n", [Pid]),
register(glavni, Pid),
Pid1 = spawn (prvi@Molly, mi, loop_prvi, []),
io:format("Prvi PID: ~w~n", [Pid1]),
global:register_name (prvi, Pid1),
When I run the code, it doesn't throw any error but when I try whereis(process) I get undefined on a node that spawned it.
Here's what process' console says:
Pid = spawn(mi, loop, [X]),
io:format("Glavni PID: ~w~n", [Pid]),
register(glavni, Pid),
Pid1 = spawn (prvi@Molly, mi, loop_prvi, []),
io:format("Prvi PID: ~w~n", [Pid1]),
global:register_name (prvi, Pid1),
And when I try to whereis(process) from any node, either the master node or the node I created the process on, it says:
(prvi@Molly)2> whereis(prvi).
undefined
(prvi@Molly)3> whereis(prvi@Molly).
undefined
回答1:
To register on several nodes you have to:
- start sevral nodes
- ensure they use the same cookie
- connect them (for example in node A, execute net_adm:ping(B))
- start a process on node B with spawn(node,...)
- register it with global:register_name(name, Pid)
- check the registration with global:whereis_name(name)
you miss at least the last point, but all of them are necessary.
来源:https://stackoverflow.com/questions/23385935/globally-registered-process-is-not-registered