# 语法错误：'await' 在异步函数外部

- **ID:** `python/asyncio-await-in-non-async`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在同步函数内或模块级别使用 await 关键字，而没有包含它的 async def，违反了 Python 语法规则。

## 版本兼容性

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

## 解决方案

1. **Define the function with async def** (100% 成功率)
   ```
   async def fetch_data(): return await some_async_func()
   ```
2. **Use asyncio.run() at module level with a wrapper** (95% 成功率)
   ```
   async def main(): await fetch_data()
if __name__ == '__main__': asyncio.run(main())
   ```

## 无效尝试

- **Decorating the function with @asyncio.coroutine** — The @asyncio.coroutine decorator is deprecated and does not make a function async; await still requires async def. (90% 失败率)
- **Using yield from instead of await** — yield from works with generators, not async functions; it will raise a different error. (70% 失败率)
