Skip to content

Tasks

Big part of the Asyncz are the Tasks. Those are special objects with instructions and parameters that are created/sent to the scheduler and then executed.

Importing a task is as simple as:

from asyncz.tasks import Task

Parameters

  • id - The unique identifier of this task.
  • name - The description of this task.
  • fn - The callable function to execute.
  • args - Positional arguments to the callable.
  • kwargs - Keyword arguments to the callable.
  • coalesce - Whether to only run the task once when several run times are due.
  • trigger - The trigger object that controls the schedule of this task.
  • executor - The name of the executor that will run this task.
  • mistrigger_grace_time - The time (in seconds) how much this task's execution is allowed to be late (None means "allow the task to run no matter how late it is").
  • max_instances - The maximum number of concurrently executing instances allowed for this task.
  • next_run_time - The next scheduled run time of this task.

Create a task

Creating a task is as simple as:

from asyncz.schedulers import AsyncIOScheduler
from asyncz.tasks import Task
from asyncz.triggers import CronTrigger

# Create a scheduler
scheduler = AsyncIOScheduler()


def check_status():
    # Logic to check statuses
    ...


Task(
    id="my-task",
    fn=check_status,
    name="my-func",
    scheduler=scheduler,
    trigger=CronTrigger(day_of_week="mon,tue,wed,thu,fri,sat,sun", hour=8, minute=1),
    max_instances=3,
    coalesce=True,
)

scheduler.start()

Update a task

You can also update a specific task and its properties directly.

from asyncz.schedulers import AsyncIOScheduler
from asyncz.tasks import Task
from asyncz.triggers import CronTrigger

# Create a scheduler
scheduler = AsyncIOScheduler()


def check_status():
    # Logic to check statuses
    ...


# Create a task
task = Task(
    id="my-task",
    fn=check_status,
    name="my-func",
    scheduler=scheduler,
    trigger=CronTrigger(day_of_week="mon,tue,wed,thu,fri,sat,sun", hour=8, minute=1),
    max_instances=3,
    coalesce=True,
)

# Update the task
task.update(
    name="my-new-task-id",
    max_instances=5,
    coalesce=False,
)

Internally the task is using the given scheduler to be updated and then executed.

Warning

All attributes can be updated but the id as this is immutable.

Reschedule a task

You can also reschedule a task when need and by that what it means is changing its trigger only.

The trigger must be the alias of the trigger object.

from asyncz.schedulers import AsyncIOScheduler
from asyncz.tasks import Task
from asyncz.triggers import CronTrigger

# Create a scheduler
scheduler = AsyncIOScheduler()


def check_status():
    # Logic to check statuses
    ...


# Create a task
task = Task(
    id="my-task",
    fn=check_status,
    name="my-func",
    scheduler=scheduler,
    trigger=CronTrigger(day_of_week="mon,tue,wed,thu,fri,sat,sun", hour=8, minute=1),
    max_instances=3,
    coalesce=True,
)

# Reschedule the task
task.reschedule("my-task", trigger="cron", hour=10, minute=5)