Vaibhav
Vaibhav

Reputation: 11

How can I spawn multiple processes concurrently from Supervisor?

I have been trying to start multiple processes of the same module from my Supervisor module by changing the arguments and the id. However, I see that the children processes are being spawned in a sequential manner.

When using spawn or spawn link it works fine but then I am not calling the worker method and the processes won't be supervised. Is there any other method in the Supervisor module which will allow doing this concurrently?

Upvotes: 1

Views: 1169

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

When using Kernel.spawn/3 or Kernel.spawn_link/3 it works fine but then I am not calling the worker method and the processes won't be supervised.

There are many issues here. In the first place, we don’t have methods in , we have functions. Supervisor.Spec.worker/3 is deprecated and should not be used. One should use child specifications instead.

And, of course, you can supervise started processes with a help of Process.monitor/1 (here is an example of how core DynamicSypervisor does that,) but this is not what you actually want to do.

Heavy initialization should be put into GenServer.handle_continue/2 callback of your workers. That way it will be executed in the process and your initialization would return immediately; actually it would be somewhat “concurrent” loading, but the processes would still be sequentially ordered, making it easier to tackle with.

Upvotes: 0

Related Questions