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.2.tar.gz (12.6 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.2-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tedx_flow-0.1.2.tar.gz
  • Upload date:
  • Size: 12.6 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.2.tar.gz
Algorithm Hash digest
SHA256 017ac8db6cbacd834b69fdc79a2e5afde60d0ca89a816c5ce24299ad68125bd0
MD5 e21b978959e1e7eceeca1b35683275e0
BLAKE2b-256 065a2339e680e7987b436731561fa4a5edb2525e76f1694c4109815aed644cf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tedx_flow-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: tedx_flow-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 33324c0b802894a34b893c073960a2e2f0f51b5110a7aeb35972b27d0d0d1205
MD5 202990a186d7eb39b91392f4c7dc3b81
BLAKE2b-256 8d820b8ea8c1b84a9e7c787e10d0fd671e59a5cdd5d001385c5655b05f026185

See more details on using hashes here.

Provenance

The following attestation bundles were made for tedx_flow-0.1.2-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