Skip to content

feat(loop): add optional overlap support to allow concurrent loop executions #2771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 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
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 @@ -55,6 +55,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
58 changes: 55 additions & 3 deletions discord/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from __future__ import annotations

import asyncio
import contextvars
import datetime
import inspect
import sys
Expand All @@ -46,6 +47,9 @@
LF = TypeVar("LF", bound=_func)
FT = TypeVar("FT", bound=_func)
ET = TypeVar("ET", bound=Callable[[Any, BaseException], Awaitable[Any]])
_current_loop_ctx: contextvars.ContextVar[int] = contextvars.ContextVar(
"_current_loop_ctx", default=None
)


class SleepHandle:
Expand All @@ -59,10 +63,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 = self.loop.call_later(relative_delta, self._set_result_safe)

def wait(self) -> asyncio.Future[Any]:
return self.future
Expand Down Expand Up @@ -91,10 +99,12 @@ def __init__(
count: int | None,
reconnect: bool,
loop: asyncio.AbstractEventLoop,
overlap: bool | int,
) -> None:
self.coro: LF = coro
self.reconnect: bool = reconnect
self.loop: asyncio.AbstractEventLoop = loop
self.overlap: bool | int = overlap
self.count: int | None = count
self._current_loop = 0
self._handle: SleepHandle = MISSING
Expand All @@ -115,6 +125,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 All @@ -128,6 +139,12 @@ def __init__(
raise TypeError(
f"Expected coroutine function, not {type(self.coro).__name__!r}."
)
if not isinstance(overlap, (bool, int)):
raise TypeError("overlap must be a bool or a positive integer.")
elif not isinstance(overlap, bool) and isinstance(overlap, int):
if overlap <= 1:
raise ValueError("overlap as an integer must be greater than 1.")
self._semaphore = asyncio.Semaphore(overlap)

async def _call_loop_function(self, name: str, *args: Any, **kwargs: Any) -> None:
coro = getattr(self, f"_{name}")
Expand Down Expand Up @@ -166,7 +183,27 @@ 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)
token = _current_loop_ctx.set(self._current_loop)
if not self.overlap:
await self.coro(*args, **kwargs)
else:

async def run_with_semaphore():
async with self._semaphore:
await self.coro(*args, **kwargs)

task = asyncio.create_task(
(
self.coro(*args, **kwargs)
if self.overlap is True
else run_with_semaphore()
),
name=f"pycord-loop-{self.coro.__name__}-{self._current_loop}",
)
task.add_done_callback(self._tasks.remove)
self._tasks.append(task)

_current_loop_ctx.reset(token)
self._last_iteration_failed = False
backoff = ExponentialBackoff()
except self._valid_exception:
Expand All @@ -192,6 +229,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 +258,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 @@ -269,7 +310,11 @@ def time(self) -> list[datetime.time] | None:
@property
def current_loop(self) -> int:
"""The current iteration of the loop."""
return self._current_loop
return (
_current_loop_ctx.get()
if _current_loop_ctx.get() is not None
else self._current_loop
)

@property
def next_iteration(self) -> datetime.datetime | None:
Expand Down Expand Up @@ -738,6 +783,7 @@ def loop(
count: int | None = None,
reconnect: bool = True,
loop: asyncio.AbstractEventLoop = MISSING,
overlap: bool | int = 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 @@ -773,6 +819,11 @@ def loop(
loop: :class:`asyncio.AbstractEventLoop`
The loop to use to register the task, if not given
defaults to :func:`asyncio.get_event_loop`.
overlap: Union[:class:`bool`, :class:`int`]
Controls whether overlapping executions of the task loop are allowed.
Set to False (default) to run iterations one at a time, True for unlimited overlap, or an int to cap the number of concurrent runs.

.. versionadded:: 2.7

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

return decorator