如何在一个Supervisor下初始化两个DynamicSupervisor?遇ID重复错误
Got it, let's break down why this error is happening and fix it right away.
The Root Cause
When you define child specs using the tuple shorthand like {Module, opts}, Elixir defaults to using the module name as the :id field for the child spec. Since you're setting up two DynamicSupervisor instances without explicitly specifying an :id, both end up with the same id: DynamicSupervisor. Supervisors require every child to have a unique id to manage them properly—hence the conflict error.
The Fix
All you need to do is add a unique :id key to each of your DynamicSupervisor child specifications. You can use atoms, strings, or any term that's unique within this supervisor's child list.
Here's your corrected configuration:
children = [ {DynamicSupervisor, name: SomeName, strategy: :one_for_one, id: :some_dynamic_supervisor}, {DynamicSupervisor, name: AnotherName, strategy: :one_for_one, id: :another_dynamic_supervisor} ]
Quick Heads-Up
This rule applies to all module-based child specs, not just DynamicSupervisor. Anytime you have multiple instances of the same module under a single supervisor (like multiple GenServer instances from the same module), you need to explicitly set unique :id values to avoid duplicate id errors.
内容的提问来源于stack exchange,提问作者Mikhail Tolstoy

