# `Snakepit.Adapters.GRPCPython`
[🔗](https://github.com/nshkrdotcom/snakepit/blob/v0.13.0/lib/snakepit/adapters/grpc_python.ex#L1)

  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_listener, %{mode: :internal})

      # External access (explicit opt-in)
      Application.put_env(:snakepit, :grpc_listener, %{
        mode: :external,
        host: "localhost",
        port: 50051
      })

  Worker ports are OS-assigned (ephemeral) and reported back during startup.

## 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 ->
      handle_chunk(chunk)
    end)
    
    # Stream large dataset processing with progress
    Snakepit.execute_stream("process_large_dataset", %{
      total_rows: 10000,
      chunk_size: 500
    }, fn chunk ->
      handle_progress(chunk)
    end)

# `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 the readiness file
(`SNAKEPIT_READY_FILE`).

# `grpc_available?`

Check if gRPC dependencies are available at runtime.

# `grpc_execute`

Execute a command via gRPC.

# `grpc_execute_stream`

Execute a streaming command via gRPC with callback.

# `init_grpc_connection`

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 readiness 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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
