{
  "id": "python/asyncio-semaphore-bound-to-different-event-loop",
  "signature": "RuntimeError: <asyncio.locks.Semaphore object at 0x...> is bound to a different event loop",
  "signature_zh": "RuntimeError: <asyncio.locks.Semaphore object at 0x...> 绑定到了不同的事件循环",
  "regex": "RuntimeError:\\ <asyncio\\.locks\\.Semaphore\\ object\\ at\\ 0x\\.\\.\\.>\\ is\\ bound\\ to\\ a\\ different\\ event\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A synchronization primitive (Semaphore, Lock, Event) created in one event loop is used in another, typically when a module-level primitive is reused across asyncio.run() calls or threads.",
  "root_cause_type": "generic",
  "root_cause_zh": "在一个事件循环中创建的同步原语（Semaphore、Lock、Event）在另一个循环中使用，通常发生在跨 asyncio.run() 调用或跨线程复用模块级原语时。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Touching private attributes breaks invariants and usually still fails on the next acquire.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The primitive is unusable in the new loop; the exception recurs on every call.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "async def main():\n    sem = asyncio.Semaphore(10)  # created inside running loop\n    await asyncio.gather(*(worker(sem) for _ in range(100)))\n\nasyncio.run(main())",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "loop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\nsem = asyncio.Semaphore(10)  # created once\nloop.run_until_complete(main(sem))\n# subsequent runs reuse the same loop",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-11",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}