From 47808a7e96da8028261fce0c06f841acb964d13a Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 10 May 2019 18:36:19 -0400 Subject: [PATCH] [tasks] Add Loop.restart This implementation waits until the task is done before starting it again. Closes #2075 --- discord/ext/tasks/__init__.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 44194917f..35d90d63e 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -128,9 +128,36 @@ class Loop: self._task = self.loop.create_task(self._loop(*args, **kwargs)) return self._task + def _can_be_cancelled(self): + return not self._is_being_cancelled and self._task and not self._task.done() + def cancel(self): """Cancels the internal task, if it is running.""" - if not self._is_being_cancelled and self._task and not self._task.done(): + if self._can_be_cancelled(): + self._task.cancel() + + def restart(self, *args, **kwargs): + r"""A convenience method to restart the internal start. + + .. note:: + + Due to the way this function works, the task is not + returned like :meth:`start`. + + Parameters + ------------ + \*args + The arguments to to use. + \*\*kwargs + The keyword arguments to use. + """ + + def restart_when_over(fut, *, args=args, kwargs=kwargs): + self._task.remove_done_callback(restart_when_over) + self.start(*args, **kwargs) + + if self._can_be_cancelled(): + self._task.add_done_callback(restart_when_over) self._task.cancel() def add_exception_type(self, exc):