Skip to main content

A lightweight task flow orchestration library for Python - zero dependencies, thread-safe

Project description

tedx-flow

A lightweight task flow orchestration library for Python.

Features

  • Zero Dependencies: Uses only Python standard library
  • Thread-Safe: Built-in synchronization for parallel execution
  • Simple API: Easy to define and chain tasks
  • Streaming Support: Real-time output as tasks complete
  • Dynamic Scheduling: Tasks can schedule next tasks at runtime
  • Parallel Execution: Independent tasks run concurrently

Installation

pip install tedx-flow

Quick Start

from concurrent.futures import ThreadPoolExecutor
from tedx_flow import Flow, Context, NextTask, TaskOutput

# Define tasks
def fetch_data(ctx: Context) -> TaskOutput:
    data = {"items": [1, 2, 3, 4, 5]}
    return TaskOutput(output=data, next_tasks=[NextTask("process")])

def process(ctx: Context) -> TaskOutput:
    data = ctx.get("fetch_data")
    result = sum(data["items"])
    return TaskOutput(output=result)

# Create and run flow
with ThreadPoolExecutor(max_workers=4) as executor:
    flow = Flow(executor)
    flow.add_task("fetch_data", fetch_data)
    flow.add_task("process", process)
    
    results = flow.run("fetch_data")
    print(results)  # {"process": 15}

Core Concepts

Task

A task is the basic unit of a flow. Each task:

  1. Receives a Context object (optionally with inputs)
  2. Returns a TaskOutput containing output value and optional next tasks
def my_task(ctx: Context, inputs: dict = None) -> TaskOutput:
    # Access previous task outputs
    previous_result = ctx.get("previous_task")
    
    # Access inputs passed via NextTask
    batch_size = inputs.get("batch_size", 10) if inputs else 10
    
    # Execute business logic
    result = do_something(previous_result, batch_size)
    
    # Return output and schedule next tasks
    return TaskOutput(
        output=result,
        next_tasks=[
            NextTask("next_task", inputs={"processed": True}),
            NextTask("parallel_task")  # Runs in parallel
        ]
    )

Context

Context is a shared data container between tasks:

  • ctx.set(key, value) - Store a value
  • ctx.get(key) - Get a value (blocks until available)
  • ctx.to_dict() - Export as dictionary

TaskOutput

# Terminal task (no next tasks)
TaskOutput(output="done")

# Chain to next task
TaskOutput(output=result, next_tasks=[NextTask("next_step")])

# Fan out to multiple tasks (parallel)
TaskOutput(output=result, next_tasks=[
    NextTask("branch_a"),
    NextTask("branch_b")
])

NextTask

# Simple scheduling
NextTask("task_name")

# With input parameters
NextTask("task_name", inputs={"key": "value"})

# Allow parallel instances of same task
NextTask("task_name", spawn_another=True)

Streaming

Get real-time results as tasks complete:

with ThreadPoolExecutor(max_workers=4) as executor:
    flow = Flow(executor)
    flow.add_task("task_a", task_a)
    flow.add_task("task_b", task_b)
    
    for chunk in flow.stream("task_a"):
        print(f"Task {chunk.task_id} completed: {chunk.value}")

Parallel Task Instances

Run multiple instances of the same task:

def fan_out(ctx: Context) -> TaskOutput:
    return TaskOutput(
        output="started",
        next_tasks=[
            NextTask("worker", inputs={"id": 1}, spawn_another=True),
            NextTask("worker", inputs={"id": 2}, spawn_another=True),
            NextTask("worker", inputs={"id": 3}, spawn_another=True),
        ]
    )

Error Handling

Exceptions in tasks are automatically propagated:

try:
    results = flow.run("start_task")
except Exception as e:
    print(f"Flow execution failed: {e}")

API Reference

Classes

Class Description
Flow Main orchestration engine
Context Shared state between tasks
TaskOutput Task return type
NextTask Specifies next task to run
StreamChunk Streaming output container
State Thread-safe value container

Flow Methods

Method Description
add_task(name, action) Register a task
run(start_task_id, inputs) Execute flow synchronously
stream(start_task_id, inputs) Execute with streaming output
get_context() Get flow context

Use Cases

  • Data Processing Pipelines: ETL, data cleaning, transformation
  • AI Workflows: Multi-step LLM calls, RAG pipelines
  • Batch Processing: Parallel sub-task processing
  • Business Processes: Order processing, approval workflows

License

MIT License

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

tedx_flow-0.1.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

tedx_flow-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file tedx_flow-0.1.0.tar.gz.

File metadata

  • Download URL: tedx_flow-0.1.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tedx_flow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7b8ff191ca6340fd8ae7087548ca908507809504e21f36fe782c0aebe8fc81ff
MD5 ded2ebd9c737b27e8b0cb4f2cb48fd10
BLAKE2b-256 3c79e53d9a1f6021064a227ee805f2490cd7f8cf3616ef58f5db4643d85064bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tedx_flow-0.1.0.tar.gz:

Publisher: publish.yml on aitobook/tedx-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tedx_flow-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tedx_flow-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tedx_flow-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ada02fbdbed9f318845e440bcf44d1ca96ed936579475b71ebc5ca50af624348
MD5 167c96544a94f97178a056ef633df0d3
BLAKE2b-256 321259925da114b3f036a58b5223bac1ee4903e534718428d9c752dbf16467d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tedx_flow-0.1.0-py3-none-any.whl:

Publisher: publish.yml on aitobook/tedx-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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