A general-purpose framework for routine execution with concurrency and event handling.
Project description
gpframe
A general-purpose framework for managing routine execution with concurrency, event handling, and lifecycle control.
Note: This project is in an early stage. It currently provides implementation only, with no tests yet.
install
pip install gpframe
Here is a transcription of the documentation from __init__.py.
gpframe: A general-purpose framework for routine execution with concurrency and event handling.
Note
This library is in an early stage of development. It is generally unstable: documentation and tests are incomplete, and the API may undergo breaking changes without notice.
Core Components
-
FrameBuilder (from .api.builder): Factory function to obtain a FrameBuilderType.
Accepts a routine to be executed inside a Frame.
The routine can be synchronous or asynchronous. -
FrameBuilderType (from .api.builder): API to configure the Frame name, logger, and handlers.
Calling FrameBuilderType.start() launches the Frame and returns a Frame object. -
Frame (from .api.frame): Represents the entire lifecycle of a Frame.
Provides Frame.task for accessing the Frame's execution task.
Allows writing to the request message map via Frame.request.update(). -
EventContext (from .api.contexts): Allows writing to the event_message map through EventContext.event_message.update().
-
RoutineContext (from .api.contexts): Allows writing to the routine_message map through RoutineContext.routine_message.update().
-
Outcome (from .api.outcome): Passed to the on_terminated handler after the Frame has finished (after on_close).
Provides read-only access to all message maps.
Each message map is a snapshot of its state at termination.
Message Updaters / Readers
gpframe defines four synchronized message registries and one result container.
These registries are the core mechanism for communication between the Frame, the routine, and event handlers.
Each registry is exposed as a MessageReader (read-only) or MessageUpdater (read-write), depending on context.
-
environment
- Purpose: Immutable configuration or contextual information.
- Access: Read: Frame, EventContext, RoutineContext Write: Frame (setup stage only)
- Example: constants, system configuration, resource identifiers.
-
request
- Purpose: Incoming requests or instructions that affect routine behavior.
- Access: Read: Frame, EventContext, RoutineContext Write: Frame (via Frame.request.update())
- Example: runtime parameters, control flags.
-
event_message
- Purpose: Event-driven updates produced by event handlers.
- Access: Read: Frame, RoutineContext Write: EventContext (via EventContext.event_message.update())
- Example: status events, log signals, external notifications.
-
routine_message
- Purpose: Communication channel from the routine to other components.
- Access: Read: Frame, EventContext Write: RoutineContext (via RoutineContext.routine_message.update())
- Example: progress reports, intermediate results.
-
routine_result
- Purpose: Result value of the routine execution.
- Access: Read: EventContext.routine_result Write: set internally by each routine execution
- Behavior: Updated every time the routine finishes (success, error, or cancellation). After the Frame terminates, the last value represents the final outcome.
- Special values: NO_VALUE = not yet executed, canceled, interrupted, or failed.
MessageReader API
A read-only view of a registry.
-
geta(key, default=...) -> Any
Retrieve a value by key without type checking.
If missing and no default given, raises KeyError. -
getd(key, typ: type[T], default: D) -> T | D
Retrieve a value by key and validate its type.
If missing, return the given default (which may have a different type). -
get(key, typ: type[T]) -> T
Retrieve a value by key and validate its type.
Raises KeyError if missing, TypeError if mismatched.
MessageUpdater API
A read-write view of a registry.
-
geta(key, default=...) -> Any
Retrieve a value by key without type checking.
If missing and no default given, raises KeyError. -
getd(key, typ: type[T], default: D) -> T | D
Retrieve a value by key and validate its type.
If missing, return the given default (which may have a different type). -
get(key, typ: type[T]) -> T
Retrieve a value by key and validate its type.
Raises KeyError if missing, TypeError if mismatched. -
update(key, value) -> T
Store a value under the given key, returning it. -
apply(key, typ: type[T], fn: Callable[[T], T], default=...) -> T
Atomically update a value using a function.
If missing, the default value is used (must be compatible withtyp):ctx.event_message.apply("count", int, lambda c: c + 1, default=0) -
remove(key, default=None) -> Any
Remove a key and return its value.
If absent, return the given default.
String Conversion Utilities
Both MessageReader and MessageUpdater provide the following helpers to
convert string values stored in the registry.
Common Behavior
-
If the key does not exist and no default is provided, a
KeyErroris raised. -
If a default value is provided and its type already matches the converted type of the method (e.g.,
intforstring_to_int,floatforstring_to_float,boolforstring_to_bool,strforstring),
then the default is returned directly without going throughpreporvalid. -
Otherwise, the stored value (or the default) is first converted to
strand processed. -
prepmay be given as either a single callable(str -> str)or a tuple of such callables.
If a tuple is provided, each function is applied in order to the string before conversion. -
After conversion, the resulting value is passed to
valid(if applicable).
IfvalidreturnsFalse, aValueErroris raised.
Methods
-
string(key, default=..., *, prep=..., valid=...) -> str
Retrieve the value by key, convert it withstr(...), and return it.
This acts as a string-typed getter regardless of the stored type.
The string may be preprocessed byprepand must satisfyvalid. -
string_to_int(key, default=..., *, prep=..., valid=...) -> int
Retrieve a string by key and convert it to an integer.
Supports standard prefixes (0x, 0o, 0b) viaint(string, 0).
Preprocessing and validation are applied as described above. -
string_to_float(key, default=..., *, prep=..., valid=...) -> float
Retrieve a string by key and convert it to a float.
Preprocessing and validation are applied as described above. -
string_to_bool(key, default=..., *, prep=..., true=(), false=()) -> bool
Retrieve a string by key and convert it to a boolean.
Preprocessing is applied as described above.
If neithertruenorfalsesets are provided, non-empty strings evaluate toTrue.
If onlytrueis given, returnsTrueif the string matches,Falseotherwise.
If onlyfalseis given, returnsFalseif the string matches,Trueotherwise.
If both are given, raisesValueErrorif the string does not match either.
Lifecycle and Access Rules
- All maps are thread-safe (backed by SynchronizedMapReader/SynchronizedMapUpdater).
- During Frame execution, access is restricted according to context type.
- After Frame termination, direct access is invalid and raises TerminatedError. To inspect final state, use Outcome which contains a snapshot of all maps.
Lifecycle Overview
-
on_open
- Called first at the very beginning.
-
on_start
- Called before routine execution.
-
routine
- The main processing logic.
-
on_end
- Called immediately after routine finishes.
-
on_redo
- Called right after on_end.
- If it returns True, the loop continues with on_start → routine → on_end → on_redo.
- If it returns False, the loop breaks and termination begins.
-
on_cancel (shielded)
- Called when asyncio.CancelledError is raised.
-
on_close (shielded)
- Always called at the end, regardless of success, failure, or cancellation.
-
on_terminated (shielded)
- Always called after on_close.
Control Flow (Summary)
on_open → (loop) [ on_start → routine → on_end → on_redo ] ├─ on_redo == True → loop continues └─ on_redo == False → loop ends → finally: on_close → on_terminated
※ If asyncio.CancelledError is raised during execution, on_cancel will be called first, then finally on_close → on_terminated.
Error Handling
-
If an exception occurs in any handler (on_* / routine / on_redo), it is passed to exception_handler.
- exception_handler is shielded.
-
If exception_handler returns False, the exception is re-raised (propagates upward).
-
Regardless of exceptions, on_close and on_terminated are always executed.
-
NO_VALUE (from .impl.routine.result): Initial value of EventContext.routine_result.value.
Indicates that no routine result exists (not yet executed, exception raised, etc.). -
TerminatedError (from .impl.builder): Raised when attempting to access message maps after a Frame has terminated.
In such cases, maps must be accessed via Outcome. -
FutureTimeoutError, ThreadCleanupTimeoutError (from .impl.routine.asynchronous): Timeout-related errors for asynchronous routines and thread cleanup.
-
SubprocessTimeoutError (from .impl.routine.subprocess): Timeout error for subprocess routines.
-
Throw (from .impl.handler.exception): Exception wrapper for re-throwing errors without being wrapped as HandlerError.
Useful when propagating exceptions such as asyncio.CancelledError directly.
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 gpframe-0.0.15.tar.gz.
File metadata
- Download URL: gpframe-0.0.15.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b265da6dc4354487e98b4de23fdbb27478e2fe4fd20a5d55600817ea31559c70
|
|
| MD5 |
dc4273937561b6eae8751fe7a5a8bd35
|
|
| BLAKE2b-256 |
3e69b4036cb7c5876623a0b5a5e1de019399a05a25b4a6c257580a3653937fcc
|
File details
Details for the file gpframe-0.0.15-py3-none-any.whl.
File metadata
- Download URL: gpframe-0.0.15-py3-none-any.whl
- Upload date:
- Size: 32.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9921bcdf7867446eb2b0202483d479551f4ac5c69146f83ba3a5ff9cdf345aad
|
|
| MD5 |
b485da6cbc708da3544f7eb82b4a5650
|
|
| BLAKE2b-256 |
c29db501bda3a16cebfc5e84838cd65a3cdef7f156a38dbcdccd5786cc2e10e4
|