Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9dc617d
🆕 feat(loop): add optional overlap support to allow concurrent loop e…
Lumabots Apr 18, 2025
bfda3aa
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 18, 2025
8dc95c4
Update CHANGELOG.md
Lumabots Apr 18, 2025
7dbf3a0
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 18, 2025
1f1ef8a
Update __init__.py
Lumabots Apr 18, 2025
570ae99
fix- TypeError: Loop.__init__() missing 1 required positional argumen…
Lumabots Apr 18, 2025
c8f3bb4
fix cancellation using overlap=true
Lumabots Apr 18, 2025
463a249
fix
Lumabots Apr 18, 2025
26a8ff1
fix identation
Lumabots Apr 18, 2025
253c67c
Merge branch 'master' into patch-2
Lumabots Apr 30, 2025
7e4fd6a
Update CHANGELOG.md
Lumabots May 1, 2025
1562e64
Update discord/ext/tasks/__init__.py
Lumabots May 1, 2025
734ce8a
Update __init__.py
Lumabots May 1, 2025
fc137dd
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
0898f82
Update __init__.py
Lumabots May 1, 2025
209232f
Update __init__.py
Lumabots May 1, 2025
f24682a
Update __init__.py
Lumabots May 1, 2025
49bf2b7
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
071731e
implemented a way of adding a limit of concurrency
Lumabots May 1, 2025
e2ef4a6
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
1623f10
Update __init__.py
Lumabots May 1, 2025
47c8796
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
c689c81
Update __init__.py
Lumabots May 1, 2025
ef0f28e
missing not
Lumabots May 1, 2025
0cfb111
test contextvars
Lumabots May 1, 2025
b3b1a45
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
3c255e6
Update __init__.py
Lumabots May 1, 2025
82da2d8
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
c7bb885
Update __init__.py
Lumabots May 1, 2025
64e9fb4
wrong indents
Lumabots May 1, 2025
51cb3b9
removed tasks when done
Lumabots May 1, 2025
ea7c2a2
fix first iteration current loop not working
Lumabots May 1, 2025
cf51a1a
Merge branch 'master' into overlap
Lumabots May 9, 2025
136d9c8
Merge branch 'master' into overlap
Lumabots May 22, 2025
fec336a
Update __init__.py
Lumabots May 23, 2025
18dc180
Update __init__.py
Lumabots May 23, 2025
4ecf027
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 23, 2025
2c9b234
Merge branch 'master' into overlap
Lumabots Jun 20, 2025
e8291a4
Merge branch 'master' into overlap
Lumabots Jun 25, 2025
da9877c
Merge branch 'master' into overlap
Lumabots Jul 22, 2025
bee78d3
Merge branch 'master' into overlap
Lumabots Aug 2, 2025
b1cf42c
Merge branch 'master' into overlap
Lumabots Aug 2, 2025
af62dc5
Merge branch 'master' into overlap
Lumabots Aug 30, 2025
6546270
Merge branch 'master' into overlap
Lumabots Aug 30, 2025
11c3ab5
usage of set instead of list for race condition
Lumabots Aug 30, 2025
ef8ee57
Merge branch 'master' into overlap
Lumabots Aug 30, 2025
68e4de5
Merge branch 'master' into overlap
Lumabots Aug 31, 2025
2e4895c
Update discord/ext/tasks/__init__.py
Lumabots Aug 31, 2025
68f32e4
Merge branch 'master' into overlap
Lumabots Aug 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ These changes are available on the `master` branch, but have not yet been releas
([#2714](https://github.com/Pycord-Development/pycord/pull/2714))
- Added the ability to pass a `datetime.time` object to `format_dt`
([#2747](https://github.com/Pycord-Development/pycord/pull/2747))
- Added the ability to pass an `overlap` parameter to the `loop` decorator and `Loop`
class, allowing concurrent iterations if enabled
([#2765](https://github.com/Pycord-Development/pycord/pull/2765))

### Fixed

Expand Down
26 changes: 24 additions & 2 deletions discord/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ def __init__(
relative_delta = discord.utils.compute_timedelta(dt)
self.handle = loop.call_later(relative_delta, future.set_result, True)

def _set_result_safe(self):
if not self.future.done():
self.future.set_result(True)

def recalculate(self, dt: datetime.datetime) -> None:
self.handle.cancel()
relative_delta = discord.utils.compute_timedelta(dt)
self.handle = self.loop.call_later(relative_delta, self.future.set_result, True)
self.handle = loop.call_later(relative_delta, self._set_result_safe)

def wait(self) -> asyncio.Future[Any]:
return self.future
Expand Down Expand Up @@ -91,10 +95,12 @@ def __init__(
count: int | None,
reconnect: bool,
loop: asyncio.AbstractEventLoop,
overlap: bool,
) -> None:
self.coro: LF = coro
self.reconnect: bool = reconnect
self.loop: asyncio.AbstractEventLoop = loop
self.overlap: bool = overlap
self.count: int | None = count
self._current_loop = 0
self._handle: SleepHandle = MISSING
Expand All @@ -115,6 +121,7 @@ def __init__(
self._is_being_cancelled = False
self._has_failed = False
self._stop_next_iteration = False
self._tasks: list[asyncio.Task[Any]] = []

if self.count is not None and self.count <= 0:
raise ValueError("count must be greater than 0 or None.")
Expand Down Expand Up @@ -166,7 +173,11 @@ async def _loop(self, *args: Any, **kwargs: Any) -> None:
self._last_iteration = self._next_iteration
self._next_iteration = self._get_next_sleep_time()
try:
await self.coro(*args, **kwargs)
if self.overlap:
task = asyncio.create_task(self.coro(*args, **kwargs))
self._tasks.append(task)
else:
await self.coro(*args, **kwargs)
self._last_iteration_failed = False
backoff = ExponentialBackoff()
except self._valid_exception:
Expand All @@ -192,6 +203,9 @@ async def _loop(self, *args: Any, **kwargs: Any) -> None:

except asyncio.CancelledError:
self._is_being_cancelled = True
for task in self._tasks:
task.cancel()
await asyncio.gather(*self._tasks, return_exceptions=True)
raise
except Exception as exc:
self._has_failed = True
Expand All @@ -218,6 +232,7 @@ def __get__(self, obj: T, objtype: type[T]) -> Loop[LF]:
count=self.count,
reconnect=self.reconnect,
loop=self.loop,
overlap=self.overlap,
)
copy._injected = obj
copy._before_loop = self._before_loop
Expand Down Expand Up @@ -738,6 +753,7 @@ def loop(
count: int | None = None,
reconnect: bool = True,
loop: asyncio.AbstractEventLoop = MISSING,
overlap: bool = False,
) -> Callable[[LF], Loop[LF]]:
"""A decorator that schedules a task in the background for you with
optional reconnect logic. The decorator returns a :class:`Loop`.
Expand Down Expand Up @@ -774,6 +790,11 @@ def loop(
The loop to use to register the task, if not given
defaults to :func:`asyncio.get_event_loop`.

overlap: :class:`bool`
Whether to allow the next iteration of the loop to run even if the previous one has not completed.

.. versionadded:: 2.7

Raises
------
ValueError
Expand All @@ -793,6 +814,7 @@ def decorator(func: LF) -> Loop[LF]:
time=time,
reconnect=reconnect,
loop=loop,
overlap=overlap,
)

return decorator
Loading