Skip to main content
 首页 » 编程设计

Python asyncio.sleep 语法错误

2025年05月04日16youxin

我做错了什么? 当我运行它时,

import asyncio 
def oneSecond(): 
    await asyncio.sleep(1) 

我明白了:

File "<string>", line 3 
await asyncio.sleep(1) 
            ^ 
SyntaxError: invalid syntax 

使用 Python 3.3

请您参考如下方法:

您在 Python 3.3 中使用了 await 关键字,但在 Python 3.5 之前的版本中尚未添加该关键字。您应该升级您的 Python 版本。

此外,您还必须将函数定义为 async:

async def oneSecond(): 
    await asyncio.sleep(1)