【问题标题】:Don't know why `asyncio.Lock` is not working不知道为什么 `asyncio.Lock` 不起作用
【发布时间】:2017-10-30 05:05:13
【问题描述】:

所以它的作用是每当有人在聊天室频道中键入命令!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()

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多