ExecutionClient
ExecutionClient
A context manager for executing code in an IPython kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host
|
str
|
Hostname where the code execution container is running |
'localhost'
|
port
|
int
|
Host port of the code execution container |
required |
heartbeat_interval
|
float
|
Interval in seconds between heartbeat pings. Defaults to 10. |
10
|
Example
from ipybox import ExecutionClient, ExecutionContainer
binds = {"/host/path": "example/path"}
env = {"API_KEY": "secret"}
async with ExecutionContainer(binds=binds, env=env) as container:
async with ExecutionClient(host="localhost", port=container.port) as client:
result = await client.execute("print('Hello, world!')")
print(result.text)
Hello, world!
Source code in ipybox/executor.py
kernel_id
property
The ID of the running IPython kernel.
Raises:
Type | Description |
---|---|
ValueError
|
If not connected to a kernel |
connect
async
Creates and connects to an IPython kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retries
|
int
|
Number of connection attempts. Defaults to 10. |
10
|
retry_interval
|
float
|
Delay between retries in seconds. Defaults to 1.0. |
1.0
|
Raises:
Type | Description |
---|---|
ConnectionError
|
If connection cannot be established after all retries |
Source code in ipybox/executor.py
disconnect
async
Closes the connection to the kernel and cleans up resources.
Source code in ipybox/executor.py
execute
async
execute(code: str, timeout: float = 120) -> ExecutionResult
Executes code and returns the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Code to execute |
required |
timeout
|
float
|
Maximum execution time in seconds. Defaults to 120. |
120
|
Returns:
Type | Description |
---|---|
ExecutionResult
|
ExecutionResult object |
Raises:
Type | Description |
---|---|
ExecutionError
|
If code execution raised an error |
TimeoutError
|
If execution exceeds timeout duration |
Source code in ipybox/executor.py
submit
async
Submits code for execution and returns an Execution object to track it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Python code to execute |
required |
Returns:
Type | Description |
---|---|
Execution
|
An Execution object to track the submitted code execution |
Source code in ipybox/executor.py
ExecutionResult
dataclass
Execution
Execution(client: ExecutionClient, req_id: str)
Represents a code execution in an IPython kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
ExecutionClient
|
The client instance that created this execution |
required |
req_id
|
str
|
Unique identifier for the execution request |
required |
Source code in ipybox/executor.py
result
async
result(timeout: float = 120) -> ExecutionResult
Waits for execution to complete and returns the final result.
If a timeout is reached, the kernel is interrupted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time to wait in seconds. Defaults to 120. |
120
|
Returns:
Type | Description |
---|---|
ExecutionResult
|
ExecutionResult object |
Raises:
Type | Description |
---|---|
TimeoutError
|
If execution exceeds timeout duration |
Source code in ipybox/executor.py
stream
async
stream(timeout: float = 120) -> AsyncIterator[str]
Streams the execution output text as it becomes available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time to wait in seconds. Defaults to 120. |
120
|
Yields:
Type | Description |
---|---|
AsyncIterator[str]
|
Output text chunks as they arrive |
Raises:
Type | Description |
---|---|
TimeoutError
|
If execution exceeds timeout duration |
Source code in ipybox/executor.py
ExecutionError
ConnectionError
Bases: Exception
Exception raised when connection to an IPython kernel fails.