【发布时间】:2021-07-18 19:27:06
【问题描述】:
我正在制作一个不和谐的机器人,它使用一些大数据 (500-900Mb)。
我的问题是我需要每分钟刷新(从 API 获取)数据,同时仍然响应命令。
为此,我使用了 discord 模块 discordpy 及其 discord.ext.tasks.loop 对象,但正如您想象的那样,这不是多线程的,并且机器人在从 API 获取数据时非常滞后。
为了解决这个问题,我尝试将asyncio.to_thread() 与asyncio.gather() 一起使用,但它不起作用,我的错误是:
RuntimeError: Cannot run the event loop while another loop is running.
我被这个困住了
我的代码有问题:
def q():
asyncio.run(getting_API_data)
async def main():
print("starting gather")
await asyncio.gather(
asyncio.to_thread(q),
client.run(TOKEN)
)
asyncio.run(main())
注意getting_API_data 是一个异步函数
【问题讨论】:
标签: python multithreading discord.py python-asyncio