Loops#
This module contains the loop implementations in arc
. Loops can be used to call a function repeatedly with a given interval.
CronLoop
#
CronLoop(
callback: Callable[P, Awaitable[None]],
cron_format: str,
*,
timezone: timezone = datetime.timezone.utc
)
Bases: _LoopBase[P]
A simple interval loop that runs a coroutine at a specified interval.
PARAMETER | DESCRIPTION |
---|---|
callback |
The coroutine to run at the specified interval. |
cron_format |
The cron format to use. See https://en.wikipedia.org/wiki/Cron for more information.
TYPE:
|
timezone |
The timezone to use for the cron loop. Defaults to UTC. |
RAISES | DESCRIPTION |
---|---|
ImportError
|
If the |
CroniterBadCronError
|
If the cron format is invalid. |
TypeError
|
If the passed function is not a coroutine function. |
Example
You may also use the decorator @arc.utils.cron_loop
to
create a CronLoop
from a coroutine function.
cancel
#
Cancel the loop.
If you want to stop the loop gracefully, use stop()
instead.
start
#
Start the loop at the specified interval.
PARAMETER | DESCRIPTION |
---|---|
args |
The positional arguments to pass to the coroutine every iteration.
TYPE:
|
kwargs |
The keyword arguments to pass to the coroutine every iteration.
TYPE:
|
IntervalLoop
#
IntervalLoop(
callback: Callable[P, Awaitable[None]],
*,
seconds: float | None = None,
minutes: float | None = None,
hours: float | None = None,
days: float | None = None,
run_on_start: bool = True
)
Bases: _LoopBase[P]
A simple interval loop that runs a coroutine at a specified interval.
PARAMETER | DESCRIPTION |
---|---|
callback |
The coroutine to run at the specified interval. |
seconds |
The number of seconds to wait before running the coroutine again.
TYPE:
|
minutes |
The number of minutes to wait before running the coroutine again.
TYPE:
|
hours |
The number of hours to wait before running the coroutine again.
TYPE:
|
days |
The number of days to wait before running the coroutine again.
TYPE:
|
run_on_start |
Whether to run the callback immediately after starting the loop. If set to false, the loop will wait for the specified interval before first running the callback.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no interval is specified. |
TypeError
|
If the passed function is not a coroutine function. |
Example
You may also use the decorator @arc.utils.interval_loop
to
create an IntervalLoop
from a coroutine function.
cancel
#
Cancel the loop.
If you want to stop the loop gracefully, use stop()
instead.
set_interval
#
set_interval(
*,
seconds: float | None = None,
minutes: float | None = None,
hours: float | None = None,
days: float | None = None
)
Set a new specified interval.
Note
You need to restart the loop if you want these changes to take effect immediately.
PARAMETER | DESCRIPTION |
---|---|
seconds |
The number of seconds to wait before running the coroutine again.
TYPE:
|
minutes |
The number of minutes to wait before running the coroutine again.
TYPE:
|
hours |
The number of hours to wait before running the coroutine again.
TYPE:
|
days |
The number of days to wait before running the coroutine again.
TYPE:
|
start
#
Start the loop at the specified interval.
PARAMETER | DESCRIPTION |
---|---|
args |
The positional arguments to pass to the coroutine every iteration.
TYPE:
|
kwargs |
The keyword arguments to pass to the coroutine every iteration.
TYPE:
|
cron_loop
#
cron_loop(
cron_format: str, timezone: timezone = datetime.timezone.utc
) -> Callable[[Callable[P, Awaitable[None]]], CronLoop[P]]
Decorator to create a CronLoop
out of a coroutine function.
PARAMETER | DESCRIPTION |
---|---|
cron_format |
The cron format to use. See https://en.wikipedia.org/wiki/Cron for more information.
TYPE:
|
timezone |
The timezone to use for the cron loop. Defaults to UTC. |
RETURNS | DESCRIPTION |
---|---|
Callable[[Callable[P, Awaitable[None]]], CronLoop[P]]
|
The decorator. |
RAISES | DESCRIPTION |
---|---|
ImportError
|
If the |
CroniterBadCronError
|
If the cron format is invalid. |
TypeError
|
If the decorated function is not a coroutine function. |
interval_loop
#
interval_loop(
*,
seconds: float | None = None,
minutes: float | None = None,
hours: float | None = None,
days: float | None = None,
run_on_start: bool = True
) -> Callable[[Callable[P, Awaitable[None]]], IntervalLoop[P]]
A decorator to create an IntervalLoop
out of a coroutine function.
PARAMETER | DESCRIPTION |
---|---|
seconds |
The number of seconds to wait before running the coroutine again.
TYPE:
|
minutes |
The number of minutes to wait before running the coroutine again.
TYPE:
|
hours |
The number of hours to wait before running the coroutine again.
TYPE:
|
days |
The number of days to wait before running the coroutine again.
TYPE:
|
run_on_start |
Whether to run the callback immediately after starting the loop. If set to false, the loop will wait for the specified interval before first running the callback.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable[[Callable[P, Awaitable[None]]], IntervalLoop[P]]
|
The decorator. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no interval is specified. |
TypeError
|
If the decorated function is not a coroutine function. |