Skip to main content

A Task Runner Framework

Project description

Racer

Racer is a simple Python task runner framework that supports sequential and parallel task execution. It also allows the result of one task to be passed to the next, making it flexible for various workflows.

Features

  • Task Execution: Run tasks sequentially or in parallel.
  • Task Result Propagation: Optionally pass the result of a task to the next task in the queue.
  • Multithreading Support: Execute tasks in parallel using multiple threads.
  • Customizable Tasks: Easily define custom tasks by extending the BaseTask class.

Getting Started

Basic Usage

Define your tasks using either the Task or ParallelTask class, then use the Racer class to run them sequentially.

from racer import Task, ParallelTask, Racer

def add(x: int, y: int):
    return x + y

def mul(x: int, y: int):
    return x * y

task1 = Task(name="task1", target=add, kwargs={"x": 1, "y": 5})
task2 = Task(name="task2", target=mul, args=(3, 4))

racer = Racer([task1, task2])
result = racer.run(1)
print(result)

Output:

{0: {'task1': 6, 'task2': 12}}

Running Parallel Task

To run a task in parallel, use the ParallelTask class. You can specify the number of workers (threads) to run the task concurrently.

from racer import ParallelTask

def mul(x: int, y: int):
    return x * y

parallel_task = ParallelTask(name="task3", target=mul, num_workers=3, args=(5, 6))

racer = Racer([parallel_task])
result = racer.run(1)
print(result)

Output:

{0: {'task3': [30, 30, 30]}}

Passing the Previous Task’s Result to the Next Task

To pass the result of one task to the next, set the use_prev_result flag to True when defining the task. The framework will automatically pass the previous task’s result as an argument to the next task.

def sub(x: int, y: int, prev_result=None):
    return prev_result - x - y

task1 = Task(name="task1", target=add, kwargs={"x": 1, "y": 5})
task2 = Task(name="task2", target=sub, args=(3, 4), use_prev_result=True)

racer = Racer([task1, task2])
result = racer.run(1)
print(result)

In this example, the result of task1 is passed as an additional argument to task2.

Output:

{0: {'task1': 6, 'task2': -1}}

Running with Clones

You can run the same set of tasks multiple times by passing the number of clones to the run method.

racer = Racer([task1, task2])
result = racer.run(3)
print(result)

Output:

{0: {'task1': 6, 'task2': -1}, 1: {'task1': 6, 'task2': -1}, 2: {'task1': 6, 'task2': -1}}

More Examples

from racer import ParallelTask, Racer, Task


def add(x: int, y: int):
    return x + y


def sub(x: int, y: int, z: int):
    return x - y - z


def mul(x: int, y: int, z: int):
    return x * y * z


if __name__ == "__main__":
    task1 = Task(name="task1", target=add, kwargs={"x": 1, "y": 5})
    task2 = Task(name="task2", target=sub, args=(3, 4), use_prev_result=True)
    task3 = ParallelTask(
        name="task3", target=mul, num_workers=3, args=(5, 6), use_prev_result=True
    )

    # tasks will be run sequentially
    racer = Racer([task1, task2, task3])
    results = racer.run(1)
    print(results)

Output:

{0: {'task1': 6, 'task2': -7, 'task3': [30, 30, 30]}}

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

task-racer-0.0.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

task_racer-0.0.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file task-racer-0.0.3.tar.gz.

File metadata

  • Download URL: task-racer-0.0.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for task-racer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ffd75d38c85f138a3eeec9a98070548739e3a06aa5b4e002a72ff81457d32b38
MD5 7c3248c90ae178c951c52f0616f96cf5
BLAKE2b-256 912fdb55eb486cd447f480c40b3d7786528cfb7708adc7e3ec59d31786f7a35f

See more details on using hashes here.

File details

Details for the file task_racer-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: task_racer-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for task_racer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7d48bb06453ee71da6e7bffb9aeb88c69317058e80548d1ed9d9cfec7838bc
MD5 457b0a7215d3ef21413e02cc72cebd5b
BLAKE2b-256 58231d0945005855b9452e9f2f15e477c36625a24b5510732e7a8a2f353e8e30

See more details on using hashes here.

Supported by

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