Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 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
b060522
Merge branch 'master' into overlap
Lumabots Sep 3, 2025
b3c0542
Merge branch 'master' into overlap
Lumabots Sep 17, 2025
d127829
paillat comment
Lumabots Sep 18, 2025
8311d59
Merge branch 'overlap' of https://github.com/Lumabots/pycord into pr/…
Lumabots Sep 18, 2025
4bb3a27
paillat comment
Lumabots Sep 19, 2025
38428fd
feat: add fetch_status_task to poll external service with concurrency…
Lumabots Sep 29, 2025
c63dce0
refactor: update fetch_status_task to improve logging and adjust timi…
Lumabots Sep 29, 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 @@ -106,6 +106,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))
- Added various missing channel parameters and allow `default_reaction_emoji` to be
`None`. ([#2772](https://github.com/Pycord-Development/pycord/pull/2772))
- Added support for type hinting slash command options with `typing.Annotated`.
Expand Down
66 changes: 63 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: set[asyncio.Task[Any]] = set()

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,29 @@ def __init__(
raise TypeError(
f"Expected coroutine function, not {type(self.coro).__name__!r}."
)
if isinstance(overlap, bool):
if overlap:
self._run_with_semaphore = self._run_direct
elif isinstance(overlap, int):
if overlap <= 1:
raise ValueError("overlap as an integer must be greater than 1.")
self._semaphore = asyncio.Semaphore(overlap)
self._run_with_semaphore = self._semaphore_runner_factory()
else:
raise TypeError("overlap must be a bool or a positive integer.")

async def _run_direct(self, *args: Any, **kwargs: Any) -> None:
"""Run the coroutine directly."""
await self.coro(*args, **kwargs)

def _semaphore_runner_factory(self) -> Callable[..., Awaitable[None]]:
"""Return a function that runs the coroutine with a semaphore."""

async def runner(*args: Any, **kwargs: Any) -> None:
async with self._semaphore:
await self.coro(*args, **kwargs)

return runner

async def _call_loop_function(self, name: str, *args: Any, **kwargs: Any) -> None:
coro = getattr(self, f"_{name}")
Expand Down Expand Up @@ -166,7 +200,18 @@ 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:
task = asyncio.create_task(
self._run_with_semaphore(*args, **kwargs),
name=f"pycord-loop-{self.coro.__name__}-{self._current_loop}",
)
task.add_done_callback(self._tasks.discard)
self._tasks.add(task)

_current_loop_ctx.reset(token)
self._last_iteration_failed = False
backoff = ExponentialBackoff()
except self._valid_exception:
Expand All @@ -192,6 +237,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 +266,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 +318,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 +791,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 +827,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 +852,7 @@ def decorator(func: LF) -> Loop[LF]:
time=time,
reconnect=reconnect,
loop=loop,
overlap=overlap,
)

return decorator
29 changes: 28 additions & 1 deletion examples/background_task.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import random
from datetime import time, timezone

import discord
Expand All @@ -10,7 +12,6 @@ def __init__(self, *args, **kwargs):

# An attribute we can access from our task
self.counter = 0

# Start the tasks to run in the background
self.my_background_task.start()
self.time_task.start()
Expand All @@ -37,6 +38,32 @@ async def time_task(self):
async def before_my_task(self):
await self.wait_until_ready() # Wait until the bot logs in

# Schedule every 10s; each run takes between 5 to 20s. With overlap=2, at most 2 runs
# execute concurrently so we don't build an ever-growing backlog.
@tasks.loop(seconds=10, overlap=2)
async def fetch_status_task(self):
"""
Practical overlap use-case:
Poll an external service and post a short summary. Each poll may take
between 5 to 20s due to network latency or rate limits, but we want fresh data
every 10s. Allowing a small amount of overlap avoids drifting schedules
without opening the floodgates to unlimited concurrency.
"""
print(f"[status] start run #{self.fetch_status_task.current_loop}")

# Simulate slow I/O (e.g., HTTP requests, DB queries, file I/O)
await asyncio.sleep(random.randint(5, 20))

channel = self.get_channel(1234567) # Replace with your channel ID
msg = f"[status] run #{self.fetch_status_task.current_loop} complete"
if channel:
await channel.send(msg)
else:
print(msg)

print(f"[status] end run #{self.fetch_status_task.current_loop}")


client = MyClient()
client.run("TOKEN")
Loading