{
  "id": "python/asyncio-run-called-from-running-loop",
  "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() is invoked from inside an already-running event loop, e.g. calling it in a Jupyter notebook cell or from within an async function.",
  "root_cause_type": "generic",
  "root_cause_zh": "在已运行的事件循环内部调用 asyncio.run()，例如在 Jupyter 笔记本单元格中或异步函数内部调用。",
  "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": "Raises the same error because the loop is already running.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works only if you create a fresh loop in that thread; otherwise you get 'no running event loop'.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "# in a Jupyter cell\nresult = await main()  # top-level await, no asyncio.run",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "import threading, asyncio\nresult = {}\ndef _run():\n    result['v'] = asyncio.run(main())\nt = threading.Thread(target=_run); t.start(); t.join()",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-25",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}