如何在Python函数中运行异步代码?

2024-09-30 14:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有一个Python程序使用不和谐.py我试图让函数管理不同的消息命令,但是当我把“await”放在函数中时,它给了我一个语法错误,因为它不在async中。有什么办法可以让我绕开吗?你知道吗

def selectedCommand(message):
    await client.send_message(stuff in here)

@client.event
async def on_message(message):
    selectedCommand(message)

@client.event
async def on_edit_message(message):
    selectedCommand(message)

Tags: 函数py命令程序clientevent消息message
1条回答
网友
1楼 · 发布于 2024-09-30 14:25:19

您还需要使selectedCommand成为一个协程(一个async def函数),然后在调用它时await

async def selectedCommand(message):
    await client.send_message(stuff in here)

@client.event
async def on_message(message):
    await selectedCommand(message)

相关问题 更多 >