【发布时间】:2021-11-23 03:09:11
【问题描述】:
我在 Python 中使用线程包时遇到问题。我想创建一段时间后发送消息的不和谐机器人。以前我使用time.sleep(),但它使我的机器人在给定的时间段内无用。我发现我可以使用threading.Timer(),但我不能让它工作。
示例代码:
async def send_sth(channel):
await channel.send("some message")
client = discord.Client()
@client.event
async def on_message(message):
if message.content == 'send sth':
timer = threading.Timer(10.0, send_sth, [message.channel])
timer.start()
10 秒后我有错误提示 RuntimeWarning: coroutine 'send_sth' was never awaited。是否可以使用 Timer 回调运行异步函数?
【问题讨论】:
-
为什么?为什么不简单地输入一个
await asyncio.sleep(10)?
标签: python multithreading discord.py