diff options
| author | 2018-10-06 21:22:21 +0000 | |
|---|---|---|
| committer | 2018-10-06 21:22:21 +0000 | |
| commit | 65cfc33a3e5894056f4babf857d56ab880b86dc6 (patch) | |
| tree | 4442cf1f0f88faa1b13d40413ce6242c5ca673ac /bot/utils/scheduling.py | |
| parent | Merge branch 'better-moderation' into 'master' (diff) | |
Add Reminders cog.
Diffstat (limited to '')
| -rw-r--r-- | bot/utils/scheduling.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py new file mode 100644 index 000000000..f9b844046 --- /dev/null +++ b/bot/utils/scheduling.py @@ -0,0 +1,22 @@ +import asyncio +import contextlib + + +def create_task(loop: asyncio.AbstractEventLoop, coro_or_future): + """ + Creates an asyncio.Task object from a coroutine or future object. + + :param loop: the asyncio event loop. + :param coro_or_future: the coroutine or future object to be scheduled. + """ + + task: asyncio.Task = asyncio.ensure_future(coro_or_future, loop=loop) + + # Silently ignore exceptions in a callback (handles the CancelledError nonsense) + task.add_done_callback(_silent_exception) + return task + + +def _silent_exception(future): + with contextlib.suppress(Exception): + future.exception() |