[tasks] Add way to query cancellation state for Loop.after_loop

Fixes #2121
This commit is contained in:
Rapptz
2019-04-30 01:44:50 -04:00
parent 4dee175d2a
commit 91e00d8426
2 changed files with 51 additions and 8 deletions

View File

@ -97,6 +97,37 @@ Waiting until the bot is ready before the loop starts:
print('waiting...')
await self.bot.wait_until_ready()
Doing something during cancellation:
.. code-block:: python3
from discord.ext import tasks, commands
import asyncio
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot= bot
self._batch = []
self.lock = asyncio.Lock(loop=bot.loop)
self.bulker.start()
async def do_bulk(self):
# bulk insert data here
...
@tasks.loop(seconds=10.0)
async def bulker(self):
async with self.lock:
await self.do_bulk()
@bulker.after_loop
async def on_bulker_cancel(self):
if self.bulker.is_being_cancelled() and len(self._batch) != 0:
# if we're cancelled and we have some data left...
# let's insert it to our database
await self.do_bulk()
API Reference
---------------