當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Elixir GenServer.multi_call用法及代碼示例

Elixir語言中 GenServer.multi_call 相關用法介紹如下。

用法:

multi_call(nodes \\ [node() | Node.list()], name, request, timeout \\ :infinity)
@spec multi_call([node()], name :: atom(), term(), timeout()) ::
  {replies :: [{node(), term()}], bad_nodes :: [node()]}

調用在指定的 nodes 本地注冊為 name 的所有服務器。

首先,request 被發送到 nodes 中的每個節點;然後,調用者等待回複。此函數返回一個二元素元組{replies, bad_nodes},其中:

  • replies - 是 {node, reply} 元組的列表,其中 node 是回複的節點,reply 是它的回複
  • bad_nodes - 是一個節點列表,這些節點或者不存在,或者具有給定 name 的服務器不存在或沒有回複

nodes 是請求發送到的節點名稱列表。默認值為所有已知節點(包括該節點)的列表。

為了避免遲到的答案(超時後)汙染調用者的消息隊列,中間人進程用於進行實際調用。當遲到的答案到達終止的進程時,它們將被丟棄。

例子

假設 GenServer 模塊的文檔中提到的 Stack GenServer 在 :"foo@my-machine":"bar@my-machine" 節點中注冊為 Stack

GenServer.multi_call(Stack, :pop)
#=> {[{:"foo@my-machine", :hello}, {:"bar@my-machine", :world}], []}

相關用法


注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 GenServer.multi_call(nodes \\ [node() | Node.list()], name, request, timeout \\ :infinity)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。