我定义了一个函数,该函数设置为每小时向一个不协调的通道发送一条消息。这将与典型的on_message()
命令一起运行,因此我考虑使用线程将reccuring函数放在线程内。你知道吗
我试着像你一样异步/等待线程,但是没有成功。我怎样才能使它成为可能,这样我就可以有一个消息重复每小时伴随着典型的命令?你知道吗
我的代码如下
async def reccur_func():
while 1:
await reccurship()
time.sleep(30)
client = discord.Client()
threading.Thread(target = reccur_func).start() #I tried putting await and async at the beginning of this line
@client.event
async def on_message(ctx):
......
client.run('....')
谢谢大家
看起来你可能把AsyncIO和线程技术混为一谈了。你知道吗
如果要使用AsyncIO,则需要将
reccur_func()
的任务添加到事件循环中。This other question将提供更多细节。你知道吗如果要使用线程,请从
reccur_func()
中删除await
:您可以找到有关线程in this question的更多信息。你知道吗
实现这一点的discord.py方法是使用
discord.ext.tasks
扩展将reccurship
转换为task这并不是一个确切的问题的答案,但在我的情况下是有效的。你知道吗
在discord中,我使用了
client.loop.create_task(reccur_func())
,它执行了这个函数(它自己重复),同时继续运行其他所有的函数。你知道吗相关问题 更多 >
编程相关推荐