iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0
Software Development

Elixir 多工 : Processes 與 OTP系列 第 14

14 GenServer 生命循環

  • 分享至 

  • xImage
  •  

讓我們統整一下整個 GenServer process 從啟動、接收訊息到結束的流程

啟動

啟動並有了剛開始的 state 之後,GenServer 便會自己 Loop 等待訊息

https://ithelp.ithome.com.tw/upload/images/20240928/201410544T8Low2Caj.png

接收訊息

當接收到訊息,可能是 call, cast, 或是一般的 message,就執行相對應的 callback 函式

結束後便會回到等待的狀態

https://ithelp.ithome.com.tw/upload/images/20240928/20141054PRwhfI8mDu.png

結束

當收到關閉指令或是發生錯誤,通知上級並執行 terminate callback

https://ithelp.ithome.com.tw/upload/images/20240928/20141054SZHB8gHTcH.png

mail box

如果同時有多個訊息,process 會先進先出的一個一個執行,當然,如果有很多相同又不互相依賴的工作,可以有無數個 process 同時進行,管理一批 process 就會用到下一章要講的 supervisor


上一篇
13 handle_info
系列文
Elixir 多工 : Processes 與 OTP14
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言