Skip to main content

Runevery task when it's time

Runevery (one word, as in 'bakery') is a simple async scheduling library designed to schedule tasks in no time. With a simple and intuitive API, you can plan and manage tasks to run at specific intervals, times, or even based on custom conditions. No more writing complex loops and time checks!

Installation

You can install runevery from PyPI using pip. Just run:

pip install runevery

Or, if you prefer Poetry:

poetry add runevery

For more details, check out the PyPI page.

Basic usage

Let's start with some basic usage examples to get you familiar with runevery.

Example: Fetching emails every 10 minutes

Suppose you want to fetch your emails every 10 minutes. Here's how you can do it:

from asyncio import run as asyncio_run
from runevery import run

@run.every(minutes=10)
async def fetch_emails():
    print("Very convincing code to fetch e-mails or something")

asyncio_run(run.loop())

Example: Updating weather data every hour

Need to update your weather data every hour? No problem!

from asyncio import run as asyncio_run
from runevery import run

@run.every(hours=1)
async def update_weather():
    print("Updating weather data...")

asyncio_run(run.loop())

Example: Running a task once at startup

Sometimes, you might want to run a task just once at startup. Here's how:

from asyncio import run as asyncio_run
from runevery import run

@run.once()
async def startup_task():
    print("Running startup task...")

asyncio_run(run.loop())

Example: Running a task after a specific time

Want to run a task after a specific Unix timestamp, just for fun? We have a tool for that, it's called scheduler.after:

from asyncio import run as asyncio_run
from runevery import run

@run.after(offset=1718567362)
async def run_after_timestamp():
    print("Running after specific timestamp...")

asyncio_run(run.loop())

What's a scheduler, anyway?

What is the scheduler task loop?

The scheduler and its task loop are the core of runevery. It continuously ticks through all scheduled tasks, checking if it's time to run them based on their planners.

But how does the scheduler know when to run a task? It doesn't. CREDITS ROLL Okay, but actually, it just ticks every task, and each task's planner decides if it's time to run.

What are the arguments to scheduler methods?

Let's break down the arguments you can pass to scheduler.plan, scheduler.every, scheduler.once, scheduler.at, and scheduler.after.

Example: Using scheduler.every

from asyncio import run as asyncio_run
from runevery import run

@run.every(seconds=30)
async def every_30_seconds():
    print("Running every 30 seconds")

asyncio_run(run.loop())

Example: Using scheduler.at

from asyncio import run as asyncio_run
from runevery import run

@run.at(interval=86400, hours=15)
async def daily_at_3pm():
    print("Running daily at 3 PM")

asyncio_run(run.loop())

What's a planner?

Planners are the brains behind the scheduling. They decide if a task should run based on various conditions.

Built-in planners

IntervalPlanner

Runs tasks at fixed intervals.

from runevery import IntervalPlanner, run


@run.plan(planner=IntervalPlanner(interval=60))
async def every_minute():
    print("Running every minute")

asyncio_run(run.loop())

FixedOffsetPlanner

Runs tasks at a fixed timestamp, optionally repeating at intervals.

from runevery import FixedOffsetPlanner, Scheduler

scheduler = Scheduler()

@scheduler.plan(planner=FixedOffsetPlanner(offset=1718567362, interval=3600))
async def hourly_after_timestamp():
    print("Running hourly after a specific timestamp")

asyncio_run(scheduler.loop())

CooldownPlanner

Runs tasks based on a cooldown period.

from runevery import CooldownPlanner, run

class MyCooldownSource:
    def get_cooldown(self, interval: float):
        return 0  # Replace with actual cooldown logic

@run.plan(planner=CooldownPlanner(MyCooldownSource(), interval=300))
async def cooldown_task():
    print("Running with cooldown")

asyncio_run(run.loop())

Interval strategy

The interval strategy defines if the interval is counted from the start or the end of the run.

  • Use "start" (default), when you want to count interval from the start of the previous task. The duration between callback starts doesn't drift, since it doesn't depend on the task duration.
  • Use "end", when you care about interval between tasks. This interval starts counting after the previous task has ended, so the pause between tasks doesn't drift.
from runevery import run
from asyncio import sleep

@run.every(seconds=10, interval_strategy="end")
async def very_long_task():
    await sleep(3600)
    print("This task will run each ~3610 seconds, despite the interval beint 10 seconds")

asyncio_run(run.loop())

Arguments to the task callback

Task callbacks can receive optional arguments like task and scheduler.

from runevery import run, Scheduler, SchedulingTask

@run.every(minutes=5)
async def task_with_args(task: SchedulingTask, scheduler: Scheduler):
    print(f"Task {task.final_name} running with scheduler {scheduler}")

asyncio_run(run.loop())

STOP RUNNING TASKS

Functions were not meant to be scheduled!!! Do this immediately:

from runevery import run, Scheduler

@run.once()
async def stop_everything(scheduler: Scheduler):
    for task in scheduler:
        task.discard()

asyncio_run(run.loop())

Pause a task

If you need to suspend (pause) the task execution task.pause_for and task.pause_until are for your service

from runevery import run, SchedulingTask

@run.every(minutes=5)
async def pausable_task(task: SchedulingTask):
    print("Running pausable task")
    task.pause_for(600)  # Pause for 10 minutes

asyncio_run(run.loop())

Run a task on a custom condition

Want to run a task based on a custom condition? Use a custom planner.

from runevery import run, SchedulingPlanner, SchedulingTask

class CustomConditionPlanner(SchedulingPlanner):
    def check(self, task: SchedulingTask):
        return some_custom_condition()

@run.plan(planner=CustomConditionPlanner())
async def custom_task():
    print("Running custom task")

asyncio_run(scheduler.loop())

Handle errors

Need to handle errors in your tasks? Provide an on_error callback.

from runevery import run, SchedulingTask

@run.every(minutes=5, on_error=lambda task: print(f"Error in {task.final_name}"))
async def error_prone_task():
    raise Exception("Oops!")

asyncio_run(run.loop())

And that's it! You've now got a solid understanding of how to use runevery to schedule tasks in Python. Happy scheduling!

Metadata

Release files for runevery 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for runevery 0.6.1
File Size Uploaded Provenance Actions
runevery-0.6.1.tar.gz 12.4 kB Without provenance Details for runevery-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for runevery 0.6.1
File Interpreter ABI Platform Actions
runevery-0.6.1-py3-none-any.whl Python 3 none any Details for runevery-0.6.1-py3-none-any.whl

Total release size: 24.2 kB

Release files / runevery-0.6.1.tar.gz

Download URL runevery-0.6.1.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
83cb7405a9379d1d214bae27b7237a1f07cb79387b0d5ba0f4f5fb024b34da89
BLAKE2b-256 checksum
How to use checksums
25e839942df3326ca83936e95e0ad2552d5736cb18da2f8de033c451a5bdd8f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.4

Release files / runevery-0.6.1-py3-none-any.whl

Download URL runevery-0.6.1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7a30cf79cc312048717d20fdf34ba70c052a45f990d368d22b9421f9c6c4b8c
BLAKE2b-256 checksum
How to use checksums
c196c136cac720ef89b4cad7e718bd98985009b2bcc00b1d0448e784356a0201
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.4

Security for runevery 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page