mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-20 07:54:36 +00:00
[tasks] Add Loop.stop to gracefully stop a task.
Updated docs will follow shortly.
This commit is contained in:
parent
bcdecd4e07
commit
4eead39b3c
@ -38,6 +38,7 @@ class Loop:
|
||||
self._before_loop = None
|
||||
self._after_loop = None
|
||||
self._is_being_cancelled = False
|
||||
self._stop_next_iteration = False
|
||||
|
||||
if self.count is not None and self.count <= 0:
|
||||
raise ValueError('count must be greater than 0 or None.')
|
||||
@ -73,8 +74,12 @@ class Loop:
|
||||
except self._valid_exception as exc:
|
||||
if not self.reconnect:
|
||||
raise
|
||||
if self._stop_next_iteration:
|
||||
return
|
||||
await asyncio.sleep(backoff.delay())
|
||||
else:
|
||||
if self._stop_next_iteration:
|
||||
return
|
||||
self._current_loop += 1
|
||||
if self._current_loop == self.count:
|
||||
break
|
||||
@ -87,6 +92,7 @@ class Loop:
|
||||
await self._call_loop_function('after_loop')
|
||||
self._is_being_cancelled = False
|
||||
self._current_loop = 0
|
||||
self._stop_next_iteration = False
|
||||
|
||||
def __get__(self, obj, objtype):
|
||||
if obj is None:
|
||||
@ -129,6 +135,17 @@ class Loop:
|
||||
self._task = self.loop.create_task(self._loop(*args, **kwargs))
|
||||
return self._task
|
||||
|
||||
def stop(self):
|
||||
r"""Gracefully stops the task from running.
|
||||
|
||||
Unlike :meth:`cancel`\, this allows the task to finish its
|
||||
current iteration before gracefully exiting.
|
||||
|
||||
.. versionadded:: 1.2
|
||||
"""
|
||||
if self._task and not self._task.done():
|
||||
self._stop_next_iteration = True
|
||||
|
||||
def _can_be_cancelled(self):
|
||||
return not self._is_being_cancelled and self._task and not self._task.done()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user