# RuntimeError: <asyncio.locks.Semaphore object at 0x...> 绑定到了不同的事件循环

- **ID:** `python/asyncio-semaphore-bound-to-different-event-loop`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在一个事件循环中创建的同步原语（Semaphore、Lock、Event）在另一个循环中使用，通常发生在跨 asyncio.run() 调用或跨线程复用模块级原语时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (92% 成功率)
   ```
   async def main():
    sem = asyncio.Semaphore(10)  # created inside running loop
    await asyncio.gather(*(worker(sem) for _ in range(100)))

asyncio.run(main())
   ```
2. **** (85% 成功率)
   ```
   loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
sem = asyncio.Semaphore(10)  # created once
loop.run_until_complete(main(sem))
# subsequent runs reuse the same loop
   ```

## 无效尝试

- **** — Touching private attributes breaks invariants and usually still fails on the next acquire. (90% 失败率)
- **** — The primitive is unusable in the new loop; the exception recurs on every call. (85% 失败率)
