Skip to main content

A simple asynchronous task manager using sqlalchemy as a broker and result backend

Project description

Kapusta

Kapusta is a simple asynchronous task manager that allows using SQLAlchemy as a broker and result backend (with the possibility of extension). It can work both in the main thread and scale up to multiple processes. It is designed for low-load systems where the number of tasks is small but critical.

[!WARNING]

This task manager is intended for use cases where the number of tasks is so small that there is no point in creating additional processes and integrating third-party tools (e.g., running Celery, Celery-beat, RabbitMQ for a few deferred tasks per hour).

Installation

To install Kapusta, clone the repository and install the required dependencies:

pip install kapusta

Simple Example

import asyncio

from kapusta import AlchemyCRUD, Kapusta

DB_URL = '...'

kapusta = Kapusta(
    crud=AlchemyCRUD(DB_URL)
)


@kapusta.task()
def task():
    print('test')


async def main():
    await kapusta.startup()  # At the start of the application.

    await task.launch()
    # in this example, you need to wait for the task to be completed in the main thread.
    await asyncio.sleep(1)

    await kapusta.shutdown()  # At the end of the application


asyncio.run(main())

Usage

Running a Kapusta Application

kapusta = Kapusta(
    crud=AlchemyCRUD(DB_URL),
    logger=Logger('kapusta'),
    max_tick_interval=60 * 5,
    default_overdue_time_delta=None,
    default_max_retry_attempts=0,
    default_timeout=60
)

When initializing Kapusta, the following arguments are accepted:

  • crud: An object inherited from BaseCRUD. Implements the logic of the broker and result backend.
  • logger: An object from the standard logging module.
  • max_tick_interval: Defines the maximum time between checks for pending tasks that should be executed soon.
  • default_overdue_time_delta: Passed by default when registering a task if not overridden. A timedelta with the specified time when the task will be considered overdue, None if the task should be executed in any case.
  • default_max_retry_attempts: Passed by default when registering a task if not overridden.
  • default_timeout: Passed by default when registering a task if not overridden.
await kapusta.startup()

When the application starts, an asyncio.create_task method kapusta.listening is created, which in an infinite loop asynchronously accesses the CRUD service (in the classic implementation, this is AlchemyCRUD), takes tasks from there, and launches them. Also, crud.startup() is called, necessary for setting up the result backend and broker.

Defining Tasks

A task is a synchronous or asynchronous function. To register a function as a task, you can use the kapusta.register_task method or the @kapusta.task decorator (@kapusta.task internally calls kapusta.register_task).

# Using the decorator

@kapusta.task()
def sample_task(param1, param2):
    return param1 + param2
# Using the method

def sample_task(param1, param2):
    return param1 + param2

kapusta.register_task(sample_task)

When using both methods, you can pass the following arguments:

"""
execution_mode (TaskExecutionMode, optional): The execution mode of the task
    (sync, async_, thread, or process). Defaults to Sentinel.
eta_delta (timedelta, optional): The time delta for the estimated time of
    execution. If None, the task is executed immediately. Defaults to None.
overdue_time (timedelta): Time at which a task will be considered overdue.
    Defaults to None.
max_retry_attempts (int, optional): The maximum number of retry attempts
    for the task. Defaults to Sentinel.
timeout (Seconds, optional): The maximum time allowed for task execution.
    Defaults to Sentinel.
"""

Launching Tasks

You can launch tasks using the launch or launch_now methods.

await sample_task.launch(param1=1, param2=2)
await sample_task.launch_now(param1=1, param2=2)

At this point, you can also change the task parameters by passing them in update_params:

await sample_task.launch(param1=1, param2=2, update_params={'max_retry_attempts': 1})

Retrieving Task Results and Status

You can retrieve the result of a completed task using the get_task_result method.

result = await kapusta.get_task_result(task_id)
status = await kapusta.get_task_status(task_id)

CRUD Interface

Kapusta uses a CRUD interface for database operations related to tasks. You can implement your own CRUD interface by extending the CRUDProtocol class.

from kapusta import BaseCRUD

class MyCRUD(BaseCRUD):
    # Implement the required methods
    ...

Testing

Kapusta uses pytest for testing. You can run the tests using the following command:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kapusta-0.1.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kapusta-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file kapusta-0.1.1.tar.gz.

File metadata

  • Download URL: kapusta-0.1.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.4 Windows/11

File hashes

Hashes for kapusta-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7c438a3793af790bce10cb4038e5d84d4d64bd4a3e96e4f79fa0a1b9b829b472
MD5 93f4f8b93e663bc459dd9ed8ddf45088
BLAKE2b-256 1ebc1d745e841e574ef86f2a8a57caa2b759cbb965b584d022eb4595c71dff50

See more details on using hashes here.

File details

Details for the file kapusta-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: kapusta-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.4 Windows/11

File hashes

Hashes for kapusta-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5674258b19d3fc280ce769798ae4aea7616b1f139edfae27a8f947d6347e99a6
MD5 eff01c1be2b769208ae75e05ea043490
BLAKE2b-256 8c5f7606877990cf02cc2f8d108d3cdeec4b6c9d9d5f1b491fe6a93d75b676da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page