简体   繁体   English

使用 via 元组注册 GenServer 时遇到问题。 ** (UndefinedFunctionError) function PokerServer.whereis_name/1 未定义或私有)

[英]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/2unregister_name/1whereis_name/1send/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 called Registry for locally storing names that are generated dynamically. Elixir 还附带了一个名为Registry的本地、去中心化和可扩展的注册表,用于在本地存储动态生成的名称。 Documentation 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (UndefinedFunctionError) function:ssl.cipher_suites/1 未定义或私有 - (UndefinedFunctionError) function :ssl.cipher_suites/1 is undefined or private **(UndefinedFunctionError)函数Guardian.Plug.authenticated?/ 1是未定义或私有的 - ** (UndefinedFunctionError) function Guardian.Plug.authenticated?/1 is undefined or private (UndefinedFunctionError)模型中的未定义函数change / 2 - (UndefinedFunctionError) undefined function change/2 in Model Phoenix开始抛出(UndefinedFunctionError)函数:crypto.rand_bytes / 1未定义或私有 - Phoenix started throwing (UndefinedFunctionError) function :crypto.rand_bytes/1 is undefined or private (UndefinedFunctionError)未定义的函数List.Chars.to_charlist / 1 - (UndefinedFunctionError) undefined function List.Chars.to_charlist/1 在 GenServer.start_link/3 中使用 {:via, module, term} 注册名称有什么好处? - What's the benefit of registering name using {:via, module, term} in GenServer.start_link/3? 回调函数在GenServer中运行 - Callbacks function in GenServer 通过端点将呼叫发送到GenServer - send calls via an endpoint to a GenServer 函数nil.id/0未定义或私有 - Elixir - function nil.id/0 is undefined or private - Elixir function Map.put/4 未定义或私有 - function Map.put/4 is undefined or private
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM