不知道为什么`asyncio.Lock`不起作用

时间:2017-05-29 20:47:19

标签: python python-3.x locking discord discord.py

这样做的是,每当有人在聊天室频道中输入命令!test时,它会在聊天频道中输出相应的下面的字符串。但是,我希望命令一次只能使用一次,所以我想锁定命令直到它完成:

import discord, asyncio, time

client = discord.Client()

@client.event
async def on_message(message):
    lock = Lock() # define Lock
    if message.content.lower().startswith("!test") and not lock.locked():
        lock.acquire() # Lock the !test command so it can't be used now
        await client.send_message(message.channel,'test1rgews')
        await asyncio.sleep(1)
        await client.send_message(message.channel,'test2thewf')
        await asyncio.sleep(1)
        await client.send_message(message.channel,'test3rhtvw')
        await asyncio.sleep(1)
        await client.send_message(message.channel,'test4trjyr')
        await asyncio.sleep(1)
        await client.send_message(message.channel,'test5dmuye')
        await asyncio.sleep(10)
        lock.release() # Unlock the !test command now

client.run('clienttokenhere')

但我收到NameError: name 'Lock' is not defined的错误,即使我将其定义为lock = Lock()

1 个答案:

答案 0 :(得分:0)

导入模块以使用其中的名称是不够的。您必须使用asyncio.Lockfrom asyncio import Lock