What is proper way to start supervised processes pool

genserver
processes
supervisor
Tags: #<Tag:0x00007fbccb45e540> #<Tag:0x00007fbccb458690> #<Tag:0x00007fbccb454630>

#1

I want to create processes pool, my solution is using Enum.map in init function like below . Is this proper way do that?

#Supercisor module
def init(pool_size) do
    children = [
      worker(MyApp.SomeWorker [], restart: :temporary)
    ]
    supervise(children, strategy: :simple_one_for_one)
    Enum.map(1..poolsize, fn(x) -> Supervisor.start_child(__MODULE__, args) end)   
  end

#2

When I read pool, I think of poolboy, which is used when accessing limited resources. Ecto uses poolboy for db access.

In poolboy, it is the Application that holds the pool config. It uses a named function… like start_pools(config) instead of an anonymous function. Something like this.

def start(_t, _a) do
  pools_config = [
    [name: "Pool1", mfa: {...}, size: ...],
    ...
  ]
  start_pools(pools_config)
end

def start_pools(config) do
  # do the real load of children here
end

There is also the new DynamicSupervisor which replace :simple_one_for_one. It is a little different behaviour as You pass spec when starting the child.

I have used :simple_one_for_one supervisors, but now it does send warning in dialyzer and I replaced them by DS.


#3

Enum.map is perfectly fine there, however you really should be using poolboy or DynamicSupervisor. :slight_smile: