ExecutionClient
ExecutionClient
Context manager for executing code in an IPython kernel running in an
ExecutionContainer
.
The kernel is created on entering the context and destroyed on exit.
The container's /app
directory is added to the kernel's Python path.
Code execution is stateful for a given ExecutionClient
instance. Definitions and
variables of previous executions are available to subsequent executions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port
|
int
|
Host port for the container's executor port |
required |
host
|
str
|
Hostname or IP address of the container's host |
'localhost'
|
heartbeat_interval
|
float
|
Ping interval for keeping the websocket connection to the IPython kernel alive. |
10
|
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 an IPython kernel and connects to it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retries
|
int
|
Number of connection retries. |
10
|
retry_interval
|
float
|
Delay between connection retries in seconds. |
1.0
|
Raises:
Type | Description |
---|---|
ConnectionError
|
If connection cannot be established after all retries |
Source code in ipybox/executor.py
disconnect
async
Disconnects from and deletes the running IPython kernel.
Source code in ipybox/executor.py
execute
async
execute(code: str, timeout: float = 120) -> ExecutionResult
Executes code in this client's IPython kernel and returns the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Code to execute |
required |
timeout
|
float
|
Maximum time in seconds to wait for the execution result |
120
|
Raises:
Type | Description |
---|---|
ExecutionError
|
If code execution raises an error |
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in ipybox/executor.py
submit
async
Submits code for execution in this client's IPython kernel and returns an
Execution
object for consuming the execution result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Python code to execute |
required |
Returns:
Type | Description |
---|---|
Execution
|
A |
Source code in ipybox/executor.py
ExecutionResult
dataclass
Execution
Execution(client: ExecutionClient, req_id: str)
A code execution in an IPython kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
ExecutionClient
|
The client that initiated this code execution |
required |
req_id
|
str
|
Unique identifier of the code execution request |
required |
Source code in ipybox/executor.py
result
async
result(timeout: float = 120) -> ExecutionResult
Retrieves the complete result of this code execution. Waits until the result is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time in seconds to wait for the execution result |
120
|
Raises:
Type | Description |
---|---|
ExecutionError
|
If code execution raises an error |
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in ipybox/executor.py
stream
async
stream(timeout: float = 120) -> AsyncIterator[str]
Streams the code execution result as it is generated. Once the stream
is consumed, a result
is immediately
available without waiting.
Generated images are not streamed. They can be obtained from the
return value of result
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time in seconds to wait for the complete execution result |
120
|
Raises:
Type | Description |
---|---|
ExecutionError
|
If code execution raises an error |
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in ipybox/executor.py
ExecutionError
ConnectionError
Bases: Exception
Raised when a connection to an IPython kernel cannot be established.