Skip to content

Commit e47e026

Browse files
rustyconoverclaude
andcommitted
refactor: change max_processes from method to property
Changed max_processes from a method to a property across the Function class hierarchy. This makes the API more consistent since max_processes is effectively a constant per function class. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3712129 commit e47e026

10 files changed

Lines changed: 119 additions & 110 deletions

‎docs/lifecycle.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Scalar functions have a simplified lifecycle with no finalize phase. Processing
4343
│ ↓ │
4444
│ output_schema (property accessed) │
4545
│ ↓ │
46-
│ perform_init(init_batch) → InitResult │
46+
│ initialize_global_state(init_batch) → InitResult │
4747
│ ↓ │
4848
│ setup() ← Acquire resources here (DB connections, files) │
4949
│ ↓ │
@@ -71,17 +71,17 @@ Scalar functions have a simplified lifecycle with no finalize phase. Processing
7171

7272
## Multi-Process Lifecycle (max_processes > 1)
7373

74-
When `max_processes() > 1`, the client spawns multiple worker processes.
74+
When `max_processes > 1`, the client spawns multiple worker processes.
7575
One becomes the **primary worker** (runs finalize), others are **secondary workers**.
7676

7777
**Primary Worker:**
7878
```
79-
__init__ → output_schema → perform_init → setup → process → finalize → teardown
79+
__init__ → output_schema → initialize_global_state → setup → process → finalize → teardown
8080
```
8181

8282
**Secondary Workers:**
8383
```
84-
__init__ → output_schema → retrieve_init → setup → process → teardown
84+
__init__ → output_schema → load_global_state → setup → process → teardown
8585
↓
8686
(NO finalize!)
8787
```
@@ -90,8 +90,8 @@ __init__ → output_schema → retrieve_init → setup → process → teardown
9090

9191
| Aspect | Primary Worker | Secondary Workers |
9292
|--------|---------------|-------------------|
93-
| `perform_init()` called? | Yes | No |
94-
| `retrieve_init()` called? | No | Yes |
93+
| `initialize_global_state()` called? | Yes | No |
94+
| `load_global_state()` called? | No | Yes |
9595
| `finalize()` called? | Yes | No |
9696
| `teardown()` called? | Yes (after finalize) | Yes (after process ends) |
9797
| Receives all batches? | Subset (round-robin) | Subset (round-robin) |

‎docs/metadata.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SumColumnsFunction(TableInOutFunction):
1616
name = "sum_columns" # Registration name (default: snake_case of class)
1717
description = "Sum all numeric columns and return a single row"
1818
categories = ["aggregation", "numeric"]
19-
max_workers = 1 # Single-threaded (replaces max_processes())
19+
max_workers = 1 # Single-threaded (used by max_processes property)
2020
supports_distributed = True
2121

2222
columns = Arg[list]("columns", default=None, doc="Columns to sum")

‎tests/test_metadata.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ class ArgFunction(TableInOutFunction):
209209

210210

211211
class TestMaxWorkersIntegration:
212-
"""Tests for max_workers integration with max_processes()."""
212+
"""Tests for max_workers integration with max_processes property."""
213213

214214
def test_max_workers_used(self, test_logger: structlog.stdlib.BoundLogger) -> None:
215-
"""max_processes() returns Meta.max_workers when defined."""
215+
"""max_processes returns Meta.max_workers when defined."""
216216
from tests.conftest import make_invocation
217217

218218
class LimitedFunction(TableInOutFunction):
@@ -223,20 +223,20 @@ class Meta:
223223

224224
invocation = make_invocation(input_schema=pa.schema([]))
225225
func = LimitedFunction(invocation=invocation, logger=test_logger)
226-
assert func.max_processes() == 2
226+
assert func.max_processes == 2
227227

228228
def test_default_max_workers(
229229
self, test_logger: structlog.stdlib.BoundLogger
230230
) -> None:
231-
"""max_processes() returns default when max_workers not defined."""
231+
"""max_processes returns default when max_workers not defined."""
232232
from tests.conftest import make_invocation
233233

234234
class UnlimitedFunction(TableInOutFunction):
235235
data: TableInput = Arg[TableInput](0, doc="Input table") # type: ignore[assignment]
236236

237237
invocation = make_invocation(input_schema=pa.schema([]))
238238
func = UnlimitedFunction(invocation=invocation, logger=test_logger)
239-
assert func.max_processes() == 99999
239+
assert func.max_processes == 99999
240240

241241

242242
class TestArrowSerialization:

‎vgi/examples/table_in_out.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class RepeatInputsFunction(TableInOutGeneratorFunction):
183183
--------
184184
- output_schema: Returns input schema unchanged (default)
185185
- process(): For each input, yields it N times using has_more=True
186-
- max_processes(): Returns high value to enable parallel processing
186+
- max_processes: Uses default high value to enable parallel processing
187187
188188
SCHEMA TRANSFORMATION
189189
---------------------
@@ -212,7 +212,7 @@ class RepeatInputsFunction(TableInOutGeneratorFunction):
212212
----------------------------------------------
213213
This function is stateless - each batch is processed independently without
214214
any cross-batch state. This makes it trivially parallelizable using the
215-
default max_processes() from the base class.
215+
default max_processes from the base class.
216216
217217
Example:
218218
-------
@@ -428,7 +428,7 @@ class SumAllColumnsFunctionDistributed(SumAllColumnsFunction):
428428
- On GeneratorExit, each worker stores its state via store_state()
429429
- During finalize(), the primary worker collects all states via collect_states()
430430
431-
Uses the default max_processes() from the base class to enable parallelism.
431+
Uses the default max_processes from the base class to enable parallelism.
432432
433433
"""
434434

@@ -629,7 +629,7 @@ class SumAllColumnsSimpleDistributed(TableInOutFunction):
629629
4. Emit final result in finish()
630630
631631
Unlike single-process aggregations, this function:
632-
- Uses default max_processes() (allows parallelism)
632+
- Uses default max_processes (allows parallelism)
633633
- Stores partial state via save_state() before finalize
634634
- Merges all worker states via load_states() before finish()
635635

‎vgi/exceptions.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This module defines custom exceptions used throughout the VGI framework.
44
55
Classes:
6-
InitIdentifierError: Raised when init_identifier is required but not set.
6+
InitIdentifierError: Raised when execution_identifier is required but not set.
77
SchemaValidationError: Raised when a batch schema doesn't match expected schema.
88
99
"""
@@ -16,26 +16,26 @@
1616
import pyarrow as pa
1717

1818
__all__ = [
19-
"InitIdentifierError",
19+
"ExecutionIdentifierError",
2020
"SchemaValidationError",
2121
]
2222

2323

24-
class InitIdentifierError(ValueError):
25-
"""Raised when an operation requires an init_identifier that hasn't been set.
24+
class ExecutionIdentifierError(ValueError):
25+
"""Raised when an operation requires an execution_identifier that hasn't been set.
2626
2727
This typically occurs when:
28-
- store_state() is called before perform_init() or retrieve_init()
29-
- collect_states() is called before perform_init() or retrieve_init()
28+
- store_state() is called before initialize_global_state() or load_global_state()
29+
- collect_states() is called before initialize_global_state() or load_global_state()
3030
- Work queue operations are attempted before initialization
3131
32-
The init_identifier is automatically set during:
33-
- perform_init() for the primary worker
34-
- retrieve_init() for secondary workers
32+
The execution_identifier is automatically set during:
33+
- initialize_global_state() for the primary worker
34+
- load_global_state() for secondary workers
3535
3636
Resolution:
37-
- Ensure your function calls super().perform_init() in perform_init()
38-
- Ensure the worker correctly calls retrieve_init() for secondary workers
37+
- Ensure your function calls super().initialize_global_state()
38+
- Ensure the worker correctly calls load_global_state() for secondary workers
3939
4040
"""
4141

0 commit comments

Comments
 (0)