Snakepit.Adapters.GRPCPython (Snakepit v0.6.10)

View Source

gRPC-based Python adapter for Snakepit.

This adapter replaces the stdin/stdout protocol with gRPC for better performance, streaming capabilities, and more robust communication.

## Configuration

  Application.put_env(:snakepit, :adapter_module, Snakepit.Adapters.GRPCPython)
Application.put_env(:snakepit, :grpc_config, %{
  base_port: 50051,
  port_range: 100  # Will use ports 50051-50151
})

Features

  • Native streaming support for progressive results
  • HTTP/2 multiplexing for concurrent requests
  • Built-in health checks and monitoring
  • Better error handling with gRPC status codes
  • Binary data support without base64 encoding

Streaming Examples

# Stream ML inference results
Snakepit.execute_stream("batch_inference", %{
  batch_items: ["image1.jpg", "image2.jpg", "image3.jpg"]
}, fn chunk ->
  IO.puts("Processed: #{chunk["item"]} - #{chunk["confidence"]}")
end)

# Stream large dataset processing with progress
Snakepit.execute_stream("process_large_dataset", %{
  total_rows: 10000,
  chunk_size: 500
}, fn chunk ->
  IO.puts("Progress: #{chunk["progress_percent"]}%")
end)

Summary

Functions

Get the gRPC port for this adapter instance.

Check if gRPC dependencies are available at runtime.

Initialize gRPC connection for the worker. Called by GRPCWorker during initialization.

Check if this adapter uses gRPC. Returns true only if gRPC dependencies are actually available.

Functions

get_port()

Get the gRPC port for this adapter instance.

ROBUST FIX: Use port 0 to let the OS dynamically assign an available port. This completely eliminates:

  • Port collision races
  • TIME_WAIT conflicts
  • Manual port range management
  • Port leak tracking

Python will bind to an OS-assigned port and report it back via GRPC_READY.

grpc_available?()

Check if gRPC dependencies are available at runtime.

grpc_execute(connection, session_id, command, args, timeout \\ 30000)

Execute a command via gRPC.

grpc_execute_stream(connection, session_id, command, args, callback_fn, timeout \\ 300_000)

Execute a streaming command via gRPC with callback.

init_grpc_connection(port)

Initialize gRPC connection for the worker. Called by GRPCWorker during initialization.

CRITICAL FIX: This includes retry logic to handle the race condition where the Python process signals GRPC_READY before the OS socket is fully bound and accepting connections. This is common in polyglot systems where the external process startup timing is non-deterministic.

uses_grpc?()

Check if this adapter uses gRPC. Returns true only if gRPC dependencies are actually available.