()
| 255 | return "完成" |
| 256 | |
| 257 | async def demo_timeout(): |
| 258 | # 给慢操作设置 0.5 秒超时 |
| 259 | try: |
| 260 | result = await asyncio.wait_for( |
| 261 | slow_operation(), |
| 262 | timeout=0.5, |
| 263 | ) |
| 264 | print(f" 成功: {result}") |
| 265 | except asyncio.TimeoutError: |
| 266 | print(f" 超时! 操作在 0.5s 后被取消") |
| 267 | |
| 268 | print("\n 等待一个 10s 的操作,但超时设为 0.5s:") |
| 269 | asyncio.run(demo_timeout()) |
no test coverage detected