[英]Trouble registering a GenServer with a via tuple. ** (UndefinedFunctionError) function PokerServer.whereis_name/1 is undefined or private)
Here's a very simple GenServer.这是一个非常简单的 GenServer。 I am trying to register it on initialization using a via tuple.
我正在尝试使用 via 元组在初始化时注册它。
defmodule PokerServer do
use GenServer
def start(id) do
GenServer.start(__MODULE__, id, name: {:via, __MODULE__, id})
end
@impl GenServer
def init(id) do
{:ok, id}
end
end
Unless I missunderstood something in the documentation , I should be able to pass the via tuple as a name in the third argument of GenServer.start/3除非我误解了文档中的某些内容,否则我应该能够在 GenServer.start/3 的第三个参数中将 via 元组作为名称传递
Except that when I try to start the process in the iex shell, I trigger an error除了当我尝试在 iex shell 中启动进程时,我触发了一个错误
iex(1)> PokerServer.start(123)
** (UndefinedFunctionError) function PokerServer.whereis_name/1 is undefined or private
(live_draft 0.1.0) PokerServer.whereis_name(123)
(stdlib 3.14.2.2) gen.erl:83: :gen.start/6
EDIT: I use elixir 1.12.3 and erlang/OTP 23编辑:我使用 elixir 1.12.3 和 erlang/OTP 23
The module you pass in {:via, __MODULE__, id}
should export register_name/2
, unregister_name/1
, whereis_name/1
and send/2
.您传入的模块
{:via, __MODULE__, id}
应该导出register_name/2
、 unregister_name/1
、 whereis_name/1
和send/2
。 For example:例如:
def start(id) do
GenServer.start(__MODULE__, id, name: {:via, :global, id})
end
One such example is the
:global
module which uses these functions for keeping the list of names of processes and their associated PIDs that are available globally for a.network of Elixir nodes.一个这样的例子是
:global
模块,它使用这些函数来保存进程名称列表及其关联的 PID,这些 PID 在全球范围内可用于 Elixir 个节点的网络。 Elixir also ships with a local, decentralized and scalable registry calledRegistry
for locally storing names that are generated dynamically.Elixir 还附带了一个名为
Registry
的本地、去中心化和可扩展的注册表,用于在本地存储动态生成的名称。 Documentation文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.