首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python不和谐机器人:在异步函数之外不允许使用“异步”

Python不和谐机器人:在异步函数之外不允许使用“异步”
EN

Stack Overflow用户
提问于 2021-12-14 02:43:24
回答 1查看 149关注 0票数 0

我在python中制造了一个不和谐的bot,其中一些命令让bot输入了一堆废话。但是每次我都要有一个完整的异步,asyncio.sleep。

代码语言:javascript
复制
@client.command()
async def talk(ctx):
            s(3)
            async with ctx.typing():
                await asyncio.sleep(2)
                await ctx.send("Hello")
            s(2)
            async with ctx.typing():
                await asyncio.sleep(4)
                await ctx.send("My name is bob.")
            s(5)
            async with ctx.typing():
                await asyncio.sleep(3)
                await ctx.send("But first..")
            s(2)
            async with ctx.typing():
                await asyncio.sleep(10)
                await ctx.send("Tell me more about astronomy.")
            s(2)
            async with ctx.typing():
                await asyncio.sleep(5)
                await ctx.send("And about oranges.")
            s(2)
            async with ctx.typing():
                await asyncio.sleep(2)
                await ctx.send("Please")

我想加入一个独立的方法,我可以调用。就像这样:

代码语言:javascript
复制
def speak(ctx, msg, prewait=0, typewait=3):
    sleep(prewait)
    async with ctx.typing():
        await asyncio.sleep(typewait)
        await ctx.send(msg)
    return None

但这不起作用,因为我不能使用关键字异步,并在异步方法之外等待。有一个干净的解决办法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-17 16:53:53

async添加到def speak(ctx, msg, prewait=0, typewait=3)中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70343182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档