Browse Source

[tasks] Add name parameter to give the internal task a name

pull/9469/head
Rapptz 2 years ago
parent
commit
03140c0163
  1. 12
      discord/ext/tasks/__init__.py

12
discord/ext/tasks/__init__.py

@ -144,6 +144,7 @@ class Loop(Generic[LF]):
time: Union[datetime.time, Sequence[datetime.time]], time: Union[datetime.time, Sequence[datetime.time]],
count: Optional[int], count: Optional[int],
reconnect: bool, reconnect: bool,
name: Optional[str],
) -> None: ) -> None:
self.coro: LF = coro self.coro: LF = coro
self.reconnect: bool = reconnect self.reconnect: bool = reconnect
@ -165,6 +166,7 @@ class Loop(Generic[LF]):
self._is_being_cancelled = False self._is_being_cancelled = False
self._has_failed = False self._has_failed = False
self._stop_next_iteration = False self._stop_next_iteration = False
self._name: str = f'discord-ext-tasks: {coro.__qualname__}' if name is None else name
if self.count is not None and self.count <= 0: if self.count is not None and self.count <= 0:
raise ValueError('count must be greater than 0 or None.') raise ValueError('count must be greater than 0 or None.')
@ -395,7 +397,7 @@ class Loop(Generic[LF]):
args = (self._injected, *args) args = (self._injected, *args)
self._has_failed = False self._has_failed = False
self._task = asyncio.create_task(self._loop(*args, **kwargs)) self._task = asyncio.create_task(self._loop(*args, **kwargs), name=self._name)
return self._task return self._task
def stop(self) -> None: def stop(self) -> None:
@ -770,6 +772,7 @@ def loop(
time: Union[datetime.time, Sequence[datetime.time]] = MISSING, time: Union[datetime.time, Sequence[datetime.time]] = MISSING,
count: Optional[int] = None, count: Optional[int] = None,
reconnect: bool = True, reconnect: bool = True,
name: Optional[str] = None,
) -> Callable[[LF], Loop[LF]]: ) -> Callable[[LF], Loop[LF]]:
"""A decorator that schedules a task in the background for you with """A decorator that schedules a task in the background for you with
optional reconnect logic. The decorator returns a :class:`Loop`. optional reconnect logic. The decorator returns a :class:`Loop`.
@ -802,6 +805,12 @@ def loop(
Whether to handle errors and restart the task Whether to handle errors and restart the task
using an exponential back-off algorithm similar to the using an exponential back-off algorithm similar to the
one used in :meth:`discord.Client.connect`. one used in :meth:`discord.Client.connect`.
name: Optional[:class:`str`]
The name to assign to the internal task. By default
it is assigned a name based off of the callable name
such as ``discord-ext-tasks: function_name``.
.. versionadded:: 2.4
Raises Raises
-------- --------
@ -821,6 +830,7 @@ def loop(
count=count, count=count,
time=time, time=time,
reconnect=reconnect, reconnect=reconnect,
name=name,
) )
return decorator return decorator

Loading…
Cancel
Save