Last active
September 1, 2018 09:47
-
-
Save xiongxin/9eb2cee3f1174b7baadc0b55487847f5 to your computer and use it in GitHub Desktop.
Revisions
-
xiongxin revised this gist
Sep 1, 2018 . 5 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes. -
xiongxin revised this gist
Sep 1, 2018 . 6 changed files with 121 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ ``` def handle_info(msg, state) do new_state = f(state, msg) {:noreply, new_state} end ``` ``` {:noreply, new_state} {:noreply, new_state, 5_000} {:noreply, new_state, :hibernate} RETURN VAL {:stop, reason*, new_state} ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ ``` def stop(pid, reason \\ :normal, timeout \\ :infinity) do GenServer.stop(pid, reason, timeout) end ``` 回调函数 ``` def terminate(reason, state) do # Perform cleanup here # … end ``` reason参数的示例值 ``` :normal :shutdown {:shutdown, term} term ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ 来源:https://raw.githubusercontent.com/benjamintanweihao/elixir-cheatsheets/master/GenServer_CheatSheet.pdf Client ```elixir def start_link(opts \\ []) do GenServer.start_link(__MODULE__, :ok, opts) end ``` Returns ```elixir {:ok, pid} ``` Callback ``` def init(:ok) do state = init_state() {:ok, state} # 这里的 :ok 对应初始化的 :ok end ``` 回调可以返回的值 ``` {:ok, state} {ok, state, 5_000} {:ok, state, :hibernate} {:stop, reason*} RETURN VALUES :ignore ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ Client发送请求 ``` def sync_op(pid, args) do GenServer.call(pid, {:sync_op, args}) # :sync_op 是我们指定操作的名称 end ``` Callback ``` def handle_call({:sync_op, args}, from, state) do new_state = f(state, args) {:reply, new_state} end ``` 回调返回值示例 ``` {:reply, reply, new_state} {:reply, reply, new_state, 5_000} {:reply, reply, new_state, :hibernate} {:noreply, new_state} {:noreply, new_state, 5_000} {:noreply, new_state, :hibernate} {:stop, reason*, reply, new_state} {:stop, reason*, new_state} ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ `handle_cast/2 must be used for asynchronous requests, when you don’t care about a reply. A cast does not even guarantee the server has received the message and, for this reason, should be used sparingly. For example, the create/2 function we have defined in this chapter should have used call/2. We have used cast/2 for didactic purposes.` 根据文档,异步操作不保证一定能发送成功,还是尽量少使用 Client发送请求 ``` def async_op(pid, args) do GenServer.cast(pid, {:async_op, args}) end ``` Callback ``` def handle_cast({:async_op, args}, state) do new_state = f(state, args) {:noreply, new_state} end ``` 回调返回值示例 ``` {:noreply, new_state} {:noreply, new_state, 5_000} {:noreply, new_state, :hibernate} RETURN VAL {:stop, reason*, new_state} ``` -
xiongxin created this gist
Sep 1, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ Client ```elixir def start_link(opts \\ []) do GenServer.start_link(__MODULE__, :ok, opts) end ``` Returns ```elixir {:ok, pid} ```