{
  "id": "python/fastapi-background-task-event-loop",
  "signature": "RuntimeError: no running event loop",
  "signature_zh": "RuntimeError: 没有正在运行的事件循环",
  "regex": "RuntimeError:\\ no\\ running\\ event\\ loop",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "asyncio.get_event_loop() or loop.run_until_complete() is called inside a sync background task or thread where no loop is running.",
  "root_cause_type": "generic",
  "root_cause_zh": "在没有运行事件循环的同步后台任务或线程中调用了 asyncio.get_event_loop() 或 loop.run_until_complete()。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Creates leaks and doesn't share the server loop; resources bound to another loop fail.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Blocks the loop and doesn't address the missing loop in the thread.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Make the background task async:\nasync def task(): await do_work()\nbackground_tasks.add_task(task)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.82,
      "how": "In a thread, use asyncio.run(coro()) for self-contained work.",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-01-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}