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:
- Receives a
Contextobject (optionally withinputs) - Returns a
TaskOutputcontaining 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 valuectx.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
017ac8db6cbacd834b69fdc79a2e5afde60d0ca89a816c5ce24299ad68125bd0
|
|
| MD5 |
e21b978959e1e7eceeca1b35683275e0
|
|
| BLAKE2b-256 |
065a2339e680e7987b436731561fa4a5edb2525e76f1694c4109815aed644cf9
|
Provenance
The following attestation bundles were made for tedx_flow-0.1.2.tar.gz:
Publisher:
publish.yml on aitobook/tedx-flow
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tedx_flow-0.1.2.tar.gz -
Subject digest:
017ac8db6cbacd834b69fdc79a2e5afde60d0ca89a816c5ce24299ad68125bd0 - Sigstore transparency entry: 778753522
- Sigstore integration time:
-
Permalink:
aitobook/tedx-flow@49d872d0eb20b4e18ee1c92a4d670d4e6883bd99 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/aitobook
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@49d872d0eb20b4e18ee1c92a4d670d4e6883bd99 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33324c0b802894a34b893c073960a2e2f0f51b5110a7aeb35972b27d0d0d1205
|
|
| MD5 |
202990a186d7eb39b91392f4c7dc3b81
|
|
| BLAKE2b-256 |
8d820b8ea8c1b84a9e7c787e10d0fd671e59a5cdd5d001385c5655b05f026185
|
Provenance
The following attestation bundles were made for tedx_flow-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on aitobook/tedx-flow
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tedx_flow-0.1.2-py3-none-any.whl -
Subject digest:
33324c0b802894a34b893c073960a2e2f0f51b5110a7aeb35972b27d0d0d1205 - Sigstore transparency entry: 778753525
- Sigstore integration time:
-
Permalink:
aitobook/tedx-flow@49d872d0eb20b4e18ee1c92a4d670d4e6883bd99 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/aitobook
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@49d872d0eb20b4e18ee1c92a4d670d4e6883bd99 -
Trigger Event:
release
-
Statement type: