Reputation: 1637
What I want to do to is to get the record count in remote node where the record has the begining equal to <<'*fb'>>. Here is my code, however, it does not return the result. Instead, it just return the process id #PID<10878.11003.0>. How can I make it return the record count?
Node.spawn :'[email protected]', sum = fn ->
for n <- :mnesia.dirty_select(:'cz_jid_mapping',[{{:cz_jid_mapping, :'$1', :'_',:'_',:'_'},[],[:'$1']}]) do
io.format((binary_part(n,0,3)==<<"*fb">>))
end
end
Upvotes: 1
Views: 361
Reputation: 6059
The spawned process runs concurrently. Thus, if you want to send something to the caller process, you need to use messages:
me = self
Node.spawn :'[email protected]', sum = fn ->
result = ...
send(me, {:result, result})
end
receive do
{:result, result} -> {:ok, result}
after timeout_in_ms -> {:error, :timeout}
end
However, this is a lot of boilerplate, and using Node.spawn/2
should be avoided in production, since it may fail if different nodes don't have exactly the same version of the module.
It's better to implement and export some function that does the job on a local node, for example Db.record_count
. Then, you can use services from the :rpc
module to invoke that function on some other node. In particular, :rpc.call
will invoke the function and return the result:
:rpc.call(:foo@bar, Db, :record_count, [arg1, arg2, ...], timeout_in_ms)
Upvotes: 5