aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorRapptz <[email protected]>2019-04-30 01:44:50 -0400
committerRapptz <[email protected]>2019-04-30 01:45:15 -0400
commit91e00d84267f059df078a2e5132764f533472f3a (patch)
tree9aa78e718e02f3fd94428fe34b95315a5031c18d /docs
parentAdd support for voice kicking. (diff)
downloaddiscord.py-91e00d84267f059df078a2e5132764f533472f3a.tar.xz
discord.py-91e00d84267f059df078a2e5132764f533472f3a.zip
[tasks] Add way to query cancellation state for Loop.after_loop
Fixes #2121
Diffstat (limited to 'docs')
-rw-r--r--docs/ext/tasks/index.rst31
1 files changed, 31 insertions, 0 deletions
diff --git a/docs/ext/tasks/index.rst b/docs/ext/tasks/index.rst
index 2aeae796..0e9a65b9 100644
--- a/docs/ext/tasks/index.rst
+++ b/docs/ext/tasks/index.rst
@@ -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
---------------