spawn() vs. supervisor:start_child()
Thu Dec 16 09:00:57 CET 1999
Jim Larson <> wrote:
> We're building a long-running application where robustness is a
> key concern.
> The application can be a server for several clients (external
> processes, not coded in Erlang). Each client may be multithreaded,
> so each client's network connection uses a split-transaction protocol
> so that each request can be executed concurrently.
> We therefore need to launch many kinds of processes dynamically:
> - one for each new connection
> - one for each request on a connection
> - occasional dynamic processes related to the execution
> of a request, e.g. lock managers
> My question is whether we should be looking to put these dynamic
> processes in a supervision tree, or is it sufficient to link them
> to a process in the supervision tree? Which is the better style?
The better style is the one that solves your specific problem in the
best or most elegant way ;)
It all depends on how you want to treat the processes. There are
basically two separate reasons to put a process under a supervisor:
- to handle failures either by restarting the process, or to
propagate the failure in the supervision tree
- to handle dynamic code change
If you don't need these features, as might be the case for the "one
process per connection", you don't "have" to use supervisors. In this
case, if the process crashes, the connection to the client is
terminated, but nothing else is affected (unless of course the process
is linked to some other process and so on). If you're loading new
code on the fly, the dynamic processes running the old code will
continue (depending on how the code is structured; in the worst case
they might crash), but new processes will use the new code.
If you want to put dynamic processes under a supervisor, you might
want to use the 'simple_one_for_one' child type, which is optimized
for speed. If you go for the spawn() alternative, I'd recommend
proc_lib:start/start_link/spawn/spawn_link, which produce nice crash
reports on failure.
> Is it ever a good idea to dynamically create supervisors?
I'd say yes, but I have never done that, and I have never seen a
situation where I would have used them. It all depends on your
More information about the erlang-questions