diff options
| author | 2020-02-15 20:07:26 -0800 | |
|---|---|---|
| committer | 2020-02-15 20:07:26 -0800 | |
| commit | f905f73451730fb5b83b441f8d32748acef374e0 (patch) | |
| tree | 6d22b71b6534a39bf62f3742312cc1c0fcfd6c7e | |
| parent | Scheduler: fix #754 - only suppress CancelledError (diff) | |
Scheduler: remove create_task function
It's redundant because the done callback only takes a single line to add
and can be added in schedule_task().
* Use Task as the type hint rather than Future for
  _suppress_cancelled_error()
| -rw-r--r-- | bot/utils/scheduling.py | 22 | 
1 files changed, 7 insertions, 15 deletions
| diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 8d4721d70..7b055f5e7 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -2,7 +2,7 @@ import asyncio  import contextlib  import logging  from abc import abstractmethod -from typing import Coroutine, Dict, Union +from typing import Dict  from bot.utils import CogABCMeta @@ -41,7 +41,8 @@ class Scheduler(metaclass=CogABCMeta):              )              return -        task: asyncio.Task = create_task(loop, self._scheduled_task(task_data)) +        task = loop.create_task(self._scheduled_task(task_data)) +        task.add_done_callback(_suppress_cancelled_error)          self.scheduled_tasks[task_id] = task          log.debug(f"{self.cog_name}: scheduled task #{task_id}.") @@ -59,17 +60,8 @@ class Scheduler(metaclass=CogABCMeta):          del self.scheduled_tasks[task_id] -def create_task(loop: asyncio.AbstractEventLoop, coro_or_future: Union[Coroutine, asyncio.Future]) -> asyncio.Task: -    """Creates an asyncio.Task object from a coroutine or future object.""" -    task: asyncio.Task = asyncio.ensure_future(coro_or_future, loop=loop) - -    # Silently ignore CancelledError in a callback -    task.add_done_callback(_suppress_cancelled_error) -    return task - - -def _suppress_cancelled_error(future: asyncio.Future) -> None: -    """Suppress future's CancelledError exception.""" -    if future.cancelled(): +def _suppress_cancelled_error(task: asyncio.Task) -> None: +    """Suppress a task's CancelledError exception.""" +    if task.cancelled():          with contextlib.suppress(asyncio.CancelledError): -            future.exception() +            task.exception() | 
