내 봇에 알림 기능이 있습니다. 미리 알림이 지금인지 확인하기 위해 10 초마다 확인하고 현재인지 확인하면 메시지를 보냅니다.
async def reminder():
global reminderDone
if datetime.now().replace(second = 0, microsecond=0) == datetime(2021, 1, 29, 19) and not reminderDone:
reminderDone = True
cnl = bot.get_channel([channel id goes here])
await cnl.send("@everyone Reminder time")
await asyncio.sleep(10)
await reminder()
await reminder()
에서 처음 호출 됩니다 on_ready()
. 약 3 시간 후에 다음 오류가 발생합니다.
File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 21, in on_ready
await reminder()
File "bot.py", line 36, in reminder
await reminder()
File "bot.py", line 36, in reminder
await reminder()
File "bot.py", line 36, in reminder
await reminder()
[Previous line repeated 976 more times]
File "bot.py", line 35, in reminder
await asyncio.sleep(10)
File "/app/.heroku/python/lib/python3.6/asyncio/tasks.py", line 480, in sleep
future, result)
File "/app/.heroku/python/lib/python3.6/asyncio/base_events.py", line 564, in call_later
timer = self.call_at(self.time() + delay, callback, *args)
File "/app/.heroku/python/lib/python3.6/asyncio/base_events.py", line 578, in call_at
timer = events.TimerHandle(when, callback, args, self)
File "/app/.heroku/python/lib/python3.6/asyncio/events.py", line 167, in __init__
super().__init__(callback, args, loop)
File "/app/.heroku/python/lib/python3.6/asyncio/events.py", line 110, in __init__
if self._loop.get_debug():
RecursionError: maximum recursion depth exceeded
나는 이것이 함수가 자신을 호출하기 때문에 발생한다고 가정하지만 그것을 사용 await
하고 있기 때문에 함수가 실행을 멈출 때까지 기다립니다. 코 루틴 인 await
메시지 ( await cnl.send()
) 를 보내는 데 필요하기 때문에 제거 할 수는 없습니다 . 재귀 오류없이 미리 알림을 확인하기 위해 루프를 영구적으로 실행하려면 어떻게해야합니까?
같은 함수를 반복해서 호출하는 대신 간단히 루프를 만들 수 있습니다.
from discord.ext import tasks
@tasks.loop(seconds=10)
async def reminder():
global reminderDone
if datetime.now().replace(second = 0, microsecond=0) == datetime(2021, 1, 29, 19) and not reminderDone:
reminderDone = True
cnl = bot.get_channel([channel id goes here])
await cnl.send("@everyone Reminder time")
reminder.start()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다