Metadata-Version: 2.4
Name: taskex
Version: 0.3.0
Summary: A minimal dependency, pure Python library for running tasks asynchronously
Author-email: Ada Lundhe <adalundhe@lundhe.audio>
License: MIT License
        
        Copyright (c) 2025 Ada Lündhé
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Keywords: tasks,python,async,shell,executor
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic
Dynamic: license-file

# TaskEx

**A minimal-dependency, pure Python library for running async functions, sync functions, and shell commands as tracked asynchronous tasks.**

[![PyPI version](https://img.shields.io/pypi/v/taskex?color=blue)](https://pypi.org/project/taskex/)
[![License](https://img.shields.io/github/license/adalundhe/taskex)](https://github.com/adalundhe/taskex/blob/main/LICENSE)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/taskex?color=red)](https://pypi.org/project/taskex/)

| Package | TaskEx |
| --- | --- |
| Version | 0.3.0 |
| Python | >= 3.11 |
| Dependencies | `pydantic` |
| Download | https://pypi.org/project/taskex/ |
| Source | https://github.com/adalundhe/taskex |
| Keywords | async, tasks, runner, shell, jobs, executor |

---

## Features

- **Unified async interface** — Run `async def` coroutines, blocking Python functions (via `ProcessPoolExecutor`, `ThreadPoolExecutor`, or the default asyncio thread pool), and subprocess/shell commands through one `TaskRunner` API.
- **Token-based tracking** — Every execution receives a unique Snowflake-based `run.token` (`"<task_name>:<run_id>"`) for status queries, live output inspection, awaiting, or cancellation.
- **Structured Pydantic results** — Inspect runs with typed `TaskRun` and `ShellProcess` models (including status, return code, stdout/stderr, error message, traceback, and monotonic timing).
- **Non-blocking shell I/O & clean reaping** — Subprocess `stdout` and `stderr` are drained concurrently to prevent pipe deadlocks and preserve complete output across polling calls. Timeouts and cancellations terminate and reap process groups cleanly.
- **Scheduling & retention policies** — Run recurring jobs on human-readable intervals (`"500ms"`, `"30s"`, `"1h 30m"`) with fixed or infinite repeats, and bound in-memory run history using `COUNT`, `AGE`, or `COUNT_AND_AGE` cleanup policies.
- **Graceful lifecycle management** — Use `async with TaskRunner() as runner:`, `stop()` (cancel runs while keeping the runner active), `shutdown()` (graceful teardown), or `abort()` (immediate synchronous teardown).

---

## Installation

```bash
pip install taskex
```

Or with `uv`:

```bash
uv add taskex
```

---

## Quick Start

### Running an async function

```python
import asyncio
from taskex import Env, TaskRunner


async def fetch_value(x: int) -> int:
    await asyncio.sleep(0.1)
    return x * 2


async def main() -> None:
    async with TaskRunner(config=Env(MERCURY_SYNC_EXECUTOR_TYPE="none")) as runner:
        run = runner.run(fetch_value, 21)
        result = await runner.wait(run.token)
        print(result.status, result.result)  # RunStatus.COMPLETE 42


asyncio.run(main())
```

### Running a blocking sync function

Synchronous callables are automatically offloaded to the configured executor (`"process"` by default, `"thread"`, or `"none"` for asyncio's default thread pool) and concurrency-limited by `MERCURY_SYNC_TASK_RUNNER_MAX_THREADS`:

```python
import asyncio
from taskex import Env, TaskRunner


def compute_sum(n: int) -> int:
    return sum(range(n))


async def main() -> None:
    config = Env(
        MERCURY_SYNC_EXECUTOR_TYPE="thread",
        MERCURY_SYNC_TASK_RUNNER_MAX_THREADS=4,
    )
    async with TaskRunner(config=config) as runner:
        runs = [runner.run(compute_sum, 100_000) for _ in range(4)]
        results = await runner.wait_all([r.token for r in runs])
        print([r.result for r in results])


asyncio.run(main())
```

### Running a shell command

```python
import asyncio
from taskex import Env, ShellProcess, TaskRunner


async def main() -> None:
    async with TaskRunner(config=Env(MERCURY_SYNC_EXECUTOR_TYPE="none")) as runner:
        run = runner.command(
            "ls",
            "-la",
            alias="list_files",
            timeout="10s",
        )
        output: ShellProcess = await runner.wait(run.token)
        print("Exit code:", output.return_code)
        print(output.result)


asyncio.run(main())
```

### Scheduling recurring tasks

```python
import asyncio
from taskex import Env, TaskRunner


async def heartbeat() -> str:
    return "alive"


async def main() -> None:
    async with TaskRunner(config=Env(MERCURY_SYNC_EXECUTOR_TYPE="none")) as runner:
        run = runner.run(
            heartbeat,
            alias="heartbeat",
            schedule="500ms",
            repeat=3,
            keep=5,
            keep_policy="COUNT",
        )
        await runner.wait(run.token)
        await asyncio.sleep(1.5)


asyncio.run(main())
```

---

## Configuration Overview

Pass an `Env` model to `TaskRunner(instance_id=0, config=Env(...))`:

| Setting | Type | Default | Description |
| --- | --- | --- | --- |
| `MERCURY_SYNC_EXECUTOR_TYPE` | `"process" \| "thread" \| "none"` | `"process"` | Executor used for synchronous callables (`"none"` uses asyncio's default thread pool). |
| `MERCURY_SYNC_TASK_RUNNER_MAX_THREADS` | `int` (`>= 1`) | `os.cpu_count() or 1` | Maximum concurrent workers and semaphore permits for sync callables. |
| `MERCURY_SYNC_CLEANUP_INTERVAL` | `str` (duration) | `"1s"` | Interval between background cleanup passes (`COUNT` / `AGE` eviction). |
| `MERCURY_SYNC_LOG_LEVEL` | `str` | `"info"` | Log level configuration value. |

See [docs/configuration.md](docs/configuration.md) for the full configuration reference.

---

## Documentation

- [Documentation Index & Architecture](docs/index.md)
- [Getting Started & Lifecycle](docs/getting-started.md)
- [Running Async & Sync Tasks](docs/running-tasks.md)
- [Running Shell Commands](docs/shell-commands.md)
- [Scheduling, Durations & Cleanup Policies](docs/scheduling-and-cleanup.md)
- [Configuration, Executors & Snowflake IDs](docs/configuration.md)
- [API Reference](docs/api-reference.md)

## Examples

Runnable scripts are available in [`examples/`](examples/):

- [`examples/run_async_function.py`](examples/run_async_function.py)
- [`examples/run_sync_function.py`](examples/run_sync_function.py)
- [`examples/run_shell_command.py`](examples/run_shell_command.py)
- [`examples/mcp_docker_server_example/server.py`](examples/mcp_docker_server_example/server.py)

---

## Development & Testing

```bash
uv venv
uv pip install pydantic pytest pytest-asyncio
.venv/bin/python -m pytest -v tests/
```

## License

MIT — see [LICENSE](LICENSE).
