# 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.
```python
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:
```python
{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.
```python
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:
```python
{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.
```python
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:
```python
{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.
```python
racer = Racer([task1, task2])
result = racer.run(3)
print(result)
```
Output:
```python
{0: {'task1': 6, 'task2': -1}, 1: {'task1': 6, 'task2': -1}, 2: {'task1': 6, 'task2': -1}}
```
### More Examples
```python
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:
```python
{0: {'task1': 6, 'task2': -7, 'task3': [30, 30, 30]}}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/bpradana/racer",
"name": "task-racer",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "python, race condition, task, runner",
"author": "Bintang Pradana Erlangga Putra",
"author_email": "<work.bpradana@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/51/13/f27aa83d9f2cd9e2eeb65c83de27f2c100556b2fa3677b1207cedcc2a7e8/task-racer-0.0.5.tar.gz",
"platform": null,
"description": "# Racer\n\nRacer 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.\n\n## Features\n\n- **Task Execution**: Run tasks sequentially or in parallel.\n- **Task Result Propagation**: Optionally pass the result of a task to the next task in the queue.\n- **Multithreading Support**: Execute tasks in parallel using multiple threads.\n- **Customizable Tasks**: Easily define custom tasks by extending the `BaseTask` class.\n\n\n## Getting Started\n\n### Basic Usage\nDefine your tasks using either the `Task` or `ParallelTask` class, then use the `Racer` class to run them sequentially.\n\n```python\nfrom racer import Task, ParallelTask, Racer\n\ndef add(x: int, y: int):\n return x + y\n\ndef mul(x: int, y: int):\n return x * y\n\ntask1 = Task(name=\"task1\", target=add, kwargs={\"x\": 1, \"y\": 5})\ntask2 = Task(name=\"task2\", target=mul, args=(3, 4))\n\nracer = Racer([task1, task2])\nresult = racer.run(1)\nprint(result)\n```\n\nOutput:\n```python\n{0: {'task1': 6, 'task2': 12}}\n```\n\n### Running Parallel Task\nTo run a task in parallel, use the ParallelTask class. You can specify the number of workers (threads) to run the task concurrently.\n\n```python\nfrom racer import ParallelTask\n\ndef mul(x: int, y: int):\n return x * y\n\nparallel_task = ParallelTask(name=\"task3\", target=mul, num_workers=3, args=(5, 6))\n\nracer = Racer([parallel_task])\nresult = racer.run(1)\nprint(result)\n```\n\nOutput:\n```python\n{0: {'task3': [30, 30, 30]}}\n```\n\n### Passing the Previous Task\u2019s Result to the Next Task\nTo 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\u2019s result as an argument to the next task.\n\n```python\ndef sub(x: int, y: int, prev_result=None):\n return prev_result - x - y\n\ntask1 = Task(name=\"task1\", target=add, kwargs={\"x\": 1, \"y\": 5})\ntask2 = Task(name=\"task2\", target=sub, args=(3, 4), use_prev_result=True)\n\nracer = Racer([task1, task2])\nresult = racer.run(1)\nprint(result)\n```\nIn this example, the result of task1 is passed as an additional argument to task2.\n\nOutput:\n```python\n{0: {'task1': 6, 'task2': -1}}\n```\n\n### Running with Clones\nYou can run the same set of tasks multiple times by passing the number of clones to the run method.\n\n```python\nracer = Racer([task1, task2])\nresult = racer.run(3)\nprint(result)\n```\n\nOutput:\n```python\n{0: {'task1': 6, 'task2': -1}, 1: {'task1': 6, 'task2': -1}, 2: {'task1': 6, 'task2': -1}}\n```\n\n### More Examples\n```python\nfrom racer import ParallelTask, Racer, Task\n\n\ndef add(x: int, y: int):\n return x + y\n\n\ndef sub(x: int, y: int, z: int):\n return x - y - z\n\n\ndef mul(x: int, y: int, z: int):\n return x * y * z\n\n\nif __name__ == \"__main__\":\n task1 = Task(name=\"task1\", target=add, kwargs={\"x\": 1, \"y\": 5})\n task2 = Task(name=\"task2\", target=sub, args=(3, 4), use_prev_result=True)\n task3 = ParallelTask(\n name=\"task3\", target=mul, num_workers=3, args=(5, 6), use_prev_result=True\n )\n\n # tasks will be run sequentially\n racer = Racer([task1, task2, task3])\n results = racer.run(1)\n print(results)\n```\n\nOutput:\n```python\n{0: {'task1': 6, 'task2': -7, 'task3': [30, 30, 30]}}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Task Runner Framework",
"version": "0.0.5",
"project_urls": {
"Homepage": "https://github.com/bpradana/racer"
},
"split_keywords": [
"python",
" race condition",
" task",
" runner"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8f07d1a02613de47fc226e0247d1fcd3fb78f29f777680ba3000b2e5705f6fd4",
"md5": "f2ab391ce085d27d44aec1298e86ca48",
"sha256": "6485573c614d02d2dc45c7808094f1b45e5bb4084bd437da867f85d2e72ec4d7"
},
"downloads": -1,
"filename": "task_racer-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f2ab391ce085d27d44aec1298e86ca48",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4286,
"upload_time": "2024-09-18T07:20:32",
"upload_time_iso_8601": "2024-09-18T07:20:32.357656Z",
"url": "https://files.pythonhosted.org/packages/8f/07/d1a02613de47fc226e0247d1fcd3fb78f29f777680ba3000b2e5705f6fd4/task_racer-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5113f27aa83d9f2cd9e2eeb65c83de27f2c100556b2fa3677b1207cedcc2a7e8",
"md5": "370bd5405350b32d611872d0d18fbfa4",
"sha256": "3bda95a382f331919287fda5b59ee353c81b1b9612295862303e6f1c958cc4ee"
},
"downloads": -1,
"filename": "task-racer-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "370bd5405350b32d611872d0d18fbfa4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4031,
"upload_time": "2024-09-18T07:20:33",
"upload_time_iso_8601": "2024-09-18T07:20:33.555958Z",
"url": "https://files.pythonhosted.org/packages/51/13/f27aa83d9f2cd9e2eeb65c83de27f2c100556b2fa3677b1207cedcc2a7e8/task-racer-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-18 07:20:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bpradana",
"github_project": "racer",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "task-racer"
}