Skip to content

Commit c40bfcc

Browse files
feat: expose call, load shedding, and introspection in Python API
- Add call() method to Python Runtime for async request/reply. - Expose system capacity and load shedding controls in Python. - Add list_actors, actor_info, set_actor_health, and get_metrics to Python Runtime. - Export PyRequest in iris/__init__.py for type hinting and matching. - Update PyRuntime backend with missing load shedding and capacity methods. Co-authored-by: Iris Seravelle <iris.seravelle@gmail.com>
1 parent d168a91 commit c40bfcc

2 files changed

Lines changed: 88 additions & 4 deletions

File tree

iris/__init__.py

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,48 @@
1717
get_quantum_cooldown,
1818
)
1919
try:
20-
from .iris import PyRuntime, PySystemMessage, version, allocate_buffer, PyMailbox, register_offload
20+
from .iris import (
21+
PyRuntime,
22+
PySystemMessage,
23+
PyRequest,
24+
version,
25+
allocate_buffer,
26+
PyMailbox,
27+
register_offload,
28+
)
2129
except ImportError:
22-
from iris import PyRuntime, PySystemMessage, version, allocate_buffer, PyMailbox, register_offload
23-
30+
from iris import (
31+
PyRuntime,
32+
PySystemMessage,
33+
PyRequest,
34+
version,
35+
allocate_buffer,
36+
PyMailbox,
37+
register_offload,
38+
)
39+
40+
2441
class Runtime:
2542
def __init__(self):
2643
self._inner = PyRuntime()
2744

45+
def call(self, pid: int, data: Union[bytes, bytearray, memoryview], timeout: Optional[float] = None) -> Awaitable[bytes]:
46+
"""
47+
Send a request to an actor and await a response.
48+
49+
The target actor's handler will receive a `PyRequest` object (if it's a Python actor)
50+
or a `Message::Request` (if it's a Rust actor).
51+
52+
Args:
53+
pid: Target actor PID.
54+
data: Request payload.
55+
timeout: Timeout in seconds (defaults to 5.0).
56+
57+
Returns:
58+
The response payload as bytes.
59+
"""
60+
return self._inner.call(pid, data, timeout)
61+
2862
def spawn(self, handler, budget: int = 100, release_gil: bool = False) -> int:
2963
"""
3064
Spawn a new push-based actor (Green Thread).
@@ -285,6 +319,37 @@ def rollback_behavior(self, pid: int, steps: int = 1) -> int:
285319
"""
286320
return self._inner.rollback_behavior(pid, steps)
287321

322+
def list_actors(self) -> list[int]:
323+
"""List all active actor PIDs in the system."""
324+
return self._inner.list_actors()
325+
326+
def actor_info(self, pid: int) -> Optional[dict[str, str]]:
327+
"""Get detailed info about an actor as a dictionary."""
328+
return self._inner.actor_info(pid)
329+
330+
def set_actor_health(self, pid: int, status: str):
331+
"""Set health status for an actor.
332+
333+
status: 'starting', 'ready', 'busy', 'degraded'.
334+
"""
335+
self._inner.set_actor_health(pid, status)
336+
337+
def get_metrics(self) -> dict[str, int]:
338+
"""Retrieve runtime-wide metrics (actor_count, messages_sent, etc.)."""
339+
return self._inner.get_metrics()
340+
341+
def set_system_capacity(self, cap: int):
342+
"""Set the maximum number of actors allowed in the system."""
343+
self._inner.set_system_capacity(cap)
344+
345+
def set_load_shedding(self, enabled: bool):
346+
"""Enable or disable load shedding."""
347+
self._inner.set_load_shedding(enabled)
348+
349+
def is_load_shedding_active(self) -> bool:
350+
"""Check if load shedding is currently active (system at capacity)."""
351+
return self._inner.is_load_shedding_active()
352+
288353
def selective_recv(self, pid: int, matcher: Callable, timeout: Optional[float] = None) -> Awaitable[Optional[Union[bytes, PySystemMessage]]]:
289354
"""
290355
Return an awaitable that resolves when `matcher(msg)` is True.
@@ -393,4 +458,11 @@ def send_user_with_backpressure(self, pid: int, data: bytes) -> tuple[bool, Opti
393458
level = self.mailbox_backpressure(pid)
394459
return success, level
395460

396-
__all__ = ["Runtime", "PySystemMessage", "version", "allocate_buffer", "PyMailbox"]
461+
__all__ = [
462+
"Runtime",
463+
"PySystemMessage",
464+
"PyRequest",
465+
"version",
466+
"allocate_buffer",
467+
"PyMailbox",
468+
]

src/py/runtime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,18 @@ impl PyRuntime {
14061406
Ok(m)
14071407
}
14081408

1409+
fn set_system_capacity(&self, cap: u64) {
1410+
self.inner.set_system_capacity(cap);
1411+
}
1412+
1413+
fn set_load_shedding(&self, enabled: bool) {
1414+
self.inner.set_load_shedding(enabled);
1415+
}
1416+
1417+
fn is_load_shedding_active(&self) -> bool {
1418+
self.inner.is_load_shedding_active()
1419+
}
1420+
14091421
fn watch(&self, pid: u64, strategy: &str) -> PyResult<()> {
14101422
use crate::supervisor::ChildSpec;
14111423
use crate::supervisor::RestartStrategy;

0 commit comments

Comments
 (0)