{
  "id": "python/asyncio-loop-already-running-jupyter",
  "signature": "RuntimeError: asyncio.run() cannot be called from a running event loop",
  "signature_zh": "RuntimeError: 无法从正在运行的事件循环中调用 asyncio.run()",
  "regex": "RuntimeError:\\ asyncio\\.run\\(\\)\\ cannot\\ be\\ called\\ from\\ a\\ running\\ event\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "asyncio.run() was called from inside an already-running loop, common in Jupyter notebooks, IPython, or async frameworks.",
  "root_cause_type": "generic",
  "root_cause_zh": "在已运行的事件循环内部调用了 asyncio.run()，常见于 Jupyter notebook、IPython 或异步框架中。",
  "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": "Nest_asyncio patches the loop but breaks some libraries and can deadlock; often produces subtle bugs.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Raises the same error because the loop is already running.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "# In a Jupyter cell:\nawait main()\n# Do NOT use asyncio.run() in Jupyter",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "task = asyncio.create_task(main())\nawait task",
      "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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}