{
  "id": "python/asyncio-task-got-future-different-loop",
  "signature": "RuntimeError: Task got Future attached to a different loop",
  "signature_zh": "RuntimeError: 任务获取了附加到不同事件循环的 Future",
  "regex": "RuntimeError:\\ Task\\ got\\ Future\\ attached\\ to\\ a\\ different\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A coroutine or Future created under one event loop was awaited on another loop, e.g. by calling asyncio.run() twice or mixing loop.run_until_complete() with asyncio.run().",
  "root_cause_type": "generic",
  "root_cause_zh": "在某个事件循环下创建的协程或 Future 被另一个循环 await，例如两次调用 asyncio.run() 或混用 loop.run_until_complete() 与 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": "The Future object still references the original loop; identity mismatch remains.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "ensure_future still binds to the current loop, not the Future's original loop.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "loop = asyncio.new_event_loop()\nasyncio.set_event_loop(loop)\nloop.run_until_complete(main())  # single entry point",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# In Jupyter/IPython or uvicorn, just 'await main()'\n# Do NOT call asyncio.run() inside an already-running 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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-27",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}