构建和优化 Worker - Pool 应用
1. 基础功能实现
在实现 Worker - Pool 应用时,首先要完成一些基础功能,包括获取工作进程状态、实现顶级 Supervisor 以及将应用转变为 OTP 应用。
1.1 获取工作进程状态
通过以下代码可以获取可用工作进程数量和已借出(忙碌)的工作进程数量:
def handle_call(:status, _from, %{workers: workers, monitors: monitors} = state) do
{:reply, {length(workers), :ets.info(monitors, :size)}, state}
end
1.2 实现顶级 Supervisor
创建 lib/pooly/supervisor.ex 文件来实现顶级 Supervisor,代码如下:
defmodule Pooly.Supervisor do
use Supervisor
def start_link(pool_config) do
Supervisor.start_link(__MODULE__, pool_config)
end
def init(pool_config) do
children = [
worker(Pooly.Server, [self, pool_config])
]
opts =
超级会员免费看
订阅专栏 解锁全文
716

被折叠的 条评论
为什么被折叠?



