英文:
discord.py: sys:1: RuntimeWarning: coroutine 'Loop._loop' was never awaited
问题
Here's the translated traceback and code portion:
追溯(最新调用最后):
文件 "bot.py",行 55,在 <module>
send_cal.start()
文件 "python3.8/site-packages/discord/ext/tasks/__init__.py",行 398,在 start
self._task = asyncio.create_task(self._loop(*args, **kwargs))
文件 "/usr/local/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py",行 381,在 create_task
loop = events.get_running_loop()
RuntimeError: 没有正在运行的事件循环
sys:1: RuntimeWarning: 协程 'Loop._loop' 从未被等待
我使用以下代码为我的机器人获得了上述回溯:
@tasks.loop(seconds=20.0)
async def send_cal():
message_channel = bot.get_channel(target_channel_id)
print(f"Got channel {message_channel}")
df = await someApi()
df = df.drop(['stuff'], axis=1)
csv = df.to_csv(header=False, index=False)
res = csv.replace(',', ' ----> ')
if (len(res)) >= 2000:
await message.channel.send('result over 2000 chars')
await message.channel.send(res)
@send_cal.before_loop
async def before():
await client.wait_until_ready()
print("Finished waiting")
send_cal.start()
client.run('mytoken')
Please note that code translations may not include comments or inline documentation.
英文:
Traceback (most recent call last):
File "bot.py", line 55, in <module>
send_cal.start()
File "python3.8/site-packages/discord/ext/tasks/__init__.py", line 398, in start
self._task = asyncio.create_task(self._loop(*args, **kwargs))
File "/usr/local/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 381, in create_task
loop = events.get_running_loop()
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Loop._loop' was never awaited
I get the above traceback with this code for my bot
@tasks.loop(seconds=20.0)
async def send_cal():
message_channel = bot.get_channel(target_channel_id)
print(f"Got channel {message_channel}")
df = await someApi()
df = df.drop(['stuff'], axis=1)
csv = df.to_csv(header=False, index=False)
res = csv.replace(',', ' ----> ')
if (len(res)) >= 2000:
await message.channel.send('result over 2000 chars')
await message.channel.send(res)
@send_cal.before_loop
async def before():
await client.wait_until_ready()
print("Finished waiting")
send_cal.start()
client.run('mytoken')
I can't figure out why send_cal
would never be awaited. I see in examples the same setup, where .start()
kicks off the job and i assume asyncio is awaiting it under the hood, however i haven't been able to figure out why this is happening otherwise
edit: I am really just copying the starter guide here https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html
答案1
得分: 1
搞清楚了——需要在“on_ready”例程中添加send_cal.start()
。
英文:
figured it out -- needed to add send_cal.start()
to the on_ready
routine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论