AsyncSession class provides the same functionality as Session but with an async API for use in async Python code.
Constructor
Unique identifier for this session. If not provided, a random UUID is generated.
Multiple
AsyncSession objects with the same notebook_id will share the same kernel.Properties
Unique identifier for this notebook session (synchronous property).
is_connected()
Check if connected to the daemon.True if connected to daemon.kernel_started()
Check if a kernel is running.True if kernel is running.env_source()
Get environment source if kernel is running.Environment source (e.g.,
"uv:prewarmed") or None if no kernel.Connection Methods
connect()
Connect to the daemon.This is called automatically by
start_kernel() if not already connected.
Respects the RUNTIMED_SOCKET_PATH environment variable if set.start_kernel()
Start a kernel for this session.Type of kernel:
"python" or "deno".Environment source. Options:
"auto": Auto-detect from inline deps or project files"uv:prewarmed": Use prewarmed UV environment"conda:prewarmed": Use prewarmed Conda environment- For Deno kernels, this is ignored
If a kernel is already running for this session’s
notebook_id, this returns immediately without starting a new one.shutdown_kernel()
Shutdown the kernel.interrupt()
Interrupt the currently executing cell.Document Operations
create_cell()
Create a new cell in the automerge document.The cell source code.
Cell type:
"code", "markdown", or "raw".Position to insert the cell. If not provided, appends at end.
The cell ID (e.g.,
"cell-a1b2c3d4-...")set_source()
Update a cell’s source in the automerge document.The cell ID.
The new source code.
get_cell()
Get a cell from the automerge document.The cell ID.
Cell object with
id, cell_type, source, and execution_count properties.RuntimedError if cell not found.
get_cells()
Get all cells from the automerge document.List of Cell objects.
delete_cell()
Delete a cell from the automerge document.The cell ID to delete.
Execution Methods
execute_cell()
Execute a cell by ID.The cell ID to execute.
Maximum time to wait for execution.
Result with outputs, success status, and execution count.
The daemon reads the cell’s source from the automerge document and executes it. This ensures all clients see the same code being executed.If a kernel isn’t running yet, this will start one automatically.
RuntimedError if not connected, cell not found, or timeout.
run()
Convenience method: create a cell, execute it, and return the result.The code to execute.
Maximum time to wait for execution.
Result with outputs, success status, and execution count.
This is a shortcut that combines
create_cell() and execute_cell(). The cell is written to the automerge document before execution, so other connected clients will see it.queue_cell()
Queue a cell for execution without waiting for the result.The cell ID to execute.
RuntimedError if not connected or cell not found.
Async Context Manager
AsyncSession works as an async context manager for automatic cleanup:Complete Example
Parallel Execution
Useasyncio.gather() for parallel operations:
See Also
Session
Synchronous version of AsyncSession
ExecutionResult
Understanding execution results