You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在一个Supervisor下初始化两个DynamicSupervisor?遇ID重复错误

Fixing "Duplicate id: DynamicSupervisor" Error in Elixir Supervisor Configuration

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:42:42