Skip to content

Commit 9304603

Browse files
committed
improve docstrings
1 parent 1dba0d1 commit 9304603

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

discord/ext/tasks/__init__.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29+
import contextlib
2930
import datetime
3031
import inspect
3132
import logging
@@ -133,11 +134,12 @@ def __init__(
133134
self.coro: LF = coro
134135
self.reconnect: bool = reconnect
135136

136-
if create_loop is True and loop is None:
137+
if loop is None:
137138
try:
138139
loop = asyncio.get_running_loop()
139140
except RuntimeError:
140-
loop = asyncio.new_event_loop()
141+
if create_loop:
142+
loop = asyncio.new_event_loop()
141143

142144
self.loop: asyncio.AbstractEventLoop | None = loop
143145

@@ -388,6 +390,11 @@ async def __call__(self, *args: Any, **kwargs: Any) -> Any:
388390
def start(self, *args: Any, **kwargs: Any) -> asyncio.Task[None] | None:
389391
r"""Starts the internal task in the event loop.
390392
393+
If this loop was created with the ``create_loop`` parameter set as ``False`` and
394+
no running loop is found (eg this method is not called from an async context),
395+
then this task will be started automatically when any kind of :class:`~discord.Client`
396+
(subclasses included) starts.
397+
391398
Parameters
392399
------------
393400
\*args
@@ -406,6 +413,13 @@ def start(self, *args: Any, **kwargs: Any) -> asyncio.Task[None] | None:
406413
The task that has been created.
407414
"""
408415

416+
loop = None
417+
with contextlib.suppress(RuntimeError):
418+
loop = asyncio.get_running_loop()
419+
420+
if loop:
421+
self.loop = loop
422+
409423
if self.loop is None:
410424
_log.warning(
411425
f"The task {self.name} has been set to be bound to a discord.Client instance, and will start running automatically "
@@ -850,8 +864,13 @@ def loop(
850864
851865
.. versionadded:: 2.7
852866
create_loop: :class:`bool`
853-
Whether this task should create their own event loop to start running it
854-
without a client bound to it.
867+
Whether this task should create its own :class:`asyncio.AbstractEventLoop` to run if
868+
no already running one is found.
869+
870+
Loops must be in an async context in order to run, this means :meth:`Loop.start` should be
871+
called from an async context (e.g. coroutines).
872+
873+
Defaults to ``False``.
855874
856875
.. versionadded:: 2.7
857876

0 commit comments

Comments
 (0)