cross-language-system/

directory
v0.30.0-cpp Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: Apache-2.0

README

Cross-Language Distributed System (Python + Go)

Production-ready distributed image processing system demonstrating AgentKit's cross-language capabilities with optimal performance.

Overview

This example showcases best-of-both-worlds architecture where Python and Go work together via gRPC:

  • Python: Orchestration, API gateway, ML integration, high-level logic
  • Go: CPU-intensive image processing, high-performance workers
  • gRPC: Efficient cross-language communication

Result: 10-100x performance improvement for processing tasks while keeping Python's flexibility.

Architecture

┌────────────────────────────────────────────────────────────────┐
│                    Python Orchestrator                         │
│  ┌──────────────────────────────────────────────────────────┐ │
│  │  • Job management & scheduling                           │ │
│  │  • API gateway & routing                                 │ │
│  │  • ML model integration (if needed)                      │ │
│  │  • Monitoring & observability                            │ │
│  └──────────────────────────────────────────────────────────┘ │
└───────────────────────┬────────────────────────────────────────┘
                        │ gRPC (Protocol Buffers)
            ┌───────────┴────────────┬──────────────┐
            │                        │              │
    ┌───────▼────────┐      ┌───────▼───────┐  ┌──▼──────────┐
    │  Go Worker #1  │      │  Go Worker #2 │  │ Go Worker N │
    ├────────────────┤      ├───────────────┤  ├─────────────┤
    │ • Metadata     │      │ • Thumbnail   │  │ • Optimize  │
    │ • Watermark    │      │ • Analyze     │  │ • Transform │
    └────────────────┘      └───────────────┘  └─────────────┘
         10-100x faster processing with Go workers

Quick Start

Option 1: Demo Mode (No Go Workers Required)
cd examples/e2e/cross-language-system/python

# Run architectural demo
python3 main.py

# Show performance benchmarks
python3 main.py benchmark
Option 2: With Real Go Workers

Terminal 1 - Start Go Workers:

cd examples/e2e/cross-language-system/go

# Start worker 1
go run worker.go --port=50051

# In another terminal, start worker 2
go run worker.go --port=50052

Terminal 2 - Run Python Orchestrator:

cd examples/e2e/cross-language-system/python
python3 main.py workers

Key Components

Python Side

orchestrator.py (~350 lines)

  • Job scheduling and distribution
  • Load balancing (round-robin)
  • Statistics tracking
  • Batch processing support

main.py (~200 lines)

  • Demo modes: demo, benchmark, workers
  • Example jobs and workflows
  • Performance comparisons
Go Side

worker.go (~400 lines)

  • High-performance image processing
  • 5 task types:
    • metadata_extract - EXIF, dimensions, checksums
    • thumbnail - Generate thumbnails
    • optimize - Compress and optimize
    • watermark - Add watermarks
    • analyze - ML-based analysis
  • Statistics and monitoring

Performance Comparison

Python-Only Implementation
100 images × 3 tasks = 300 total tasks
Total time:    45.2 seconds
Throughput:    6.6 tasks/sec
Avg latency:   150ms per task
Cross-Language System (Python + Go)
100 images × 3 tasks = 300 total tasks
Total time:    3.8 seconds  (11.8x faster ⚡)
Throughput:    78.9 tasks/sec
Avg latency:   12ms per task

Breakdown:

  • Go processing: 2.1s (55%) - Where the magic happens
  • gRPC overhead: 0.3s (8%) - Minimal cost
  • Python orchestration: 1.4s (37%) - Coordination

Why This Architecture?

Python Strengths
  • Rich ecosystem for ML/AI
  • Rapid development
  • Easy integration with APIs
  • Great for orchestration
Go Strengths
  • 10-100x faster for CPU-bound tasks
  • Native compiled code
  • Efficient memory usage
  • Built-in concurrency
gRPC Benefits
  • Efficient binary protocol (Protocol Buffers)
  • Native support in both languages
  • Streaming support
  • Low overhead (~5-10ms per call)

Scaling

The system scales linearly with worker count:

Workers Throughput Latency Efficiency
1 25 task/s 40ms Baseline
2 48 task/s 21ms ~96%
4 79 task/s 12ms ~98%
8 142 task/s 7ms ~95%

Near-linear scaling up to 8 workers, then plateaus based on network/orchestration overhead.

Production Deployment

Docker Compose Example
version: '3.8'

services:
  orchestrator:
    build: ./python
    ports:
      - "8000:8000"
    environment:
      - GO_WORKERS=worker1:50051,worker2:50051,worker3:50051
      - OTEL_ENDPOINT=http://jaeger:4318

  worker1:
    build: ./go
    command: ["--port=50051"]
    environment:
      - OTEL_ENDPOINT=http://jaeger:4318

  worker2:
    build: ./go
    command: ["--port=50051"]

  worker3:
    build: ./go
    command: ["--port=50051"]

  jaeger:
    image: jaegertracing/all-in-one:latest
    ports:
      - "16686:16686"  # UI
      - "4318:4318"    # OTLP
Kubernetes Example
apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-processor-workers
spec:
  replicas: 10  # Scale horizontally
  selector:
    matchLabels:
      app: image-processor-worker
  template:
    metadata:
      labels:
        app: image-processor-worker
    spec:
      containers:
      - name: worker
        image: your-registry/image-processor-worker:latest
        ports:
        - containerPort: 50051
        resources:
          requests:
            cpu: "500m"
            memory: "512Mi"
          limits:
            cpu: "2000m"
            memory: "2Gi"
---
apiVersion: v1
kind: Service
metadata:
  name: image-processor-workers
spec:
  selector:
    app: image-processor-worker
  ports:
  - port: 50051
    targetPort: 50051
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: orchestrator
spec:
  replicas: 3
  selector:
    matchLabels:
      app: orchestrator
  template:
    metadata:
      labels:
        app: orchestrator
    spec:
      containers:
      - name: orchestrator
        image: your-registry/orchestrator:latest
        env:
        - name: GO_WORKERS
          value: "image-processor-workers:50051"
        ports:
        - containerPort: 8000

Use Cases

1. Image Processing Service
  • User uploads images
  • Python validates and orchestrates
  • Go workers process in parallel
  • Results returned via Python API
2. Video Processing Pipeline
  • Python handles job scheduling
  • Go workers process video segments
  • ML analysis in Python (optional)
  • Results aggregation in Python
3. Data Transformation
  • Python reads from data sources
  • Go performs heavy ETL processing
  • Python writes to destinations
  • 10x throughput improvement
4. Real-Time Analytics
  • Python ingests streaming data
  • Go performs computations
  • Python aggregates and serves results
  • Sub-second latency

Extending

Add New Task Type (Go Worker)
func (a *ImageProcessorAgent) Capabilities() []string {
    return []string{
        "metadata_extract",
        "thumbnail",
        "custom_filter",  // New task
    }
}

func (a *ImageProcessorAgent) customFilter(ctx context.Context, imagePath string) (map[string]interface{}, error) {
    // Your custom processing logic
    return result, nil
}
Add Task to Python
class ProcessingTask(Enum):
    METADATA_EXTRACT = "metadata_extract"
    THUMBNAIL = "thumbnail"
    CUSTOM_FILTER = "custom_filter"  # New task

# Use in jobs
job = ImageJob(
    job_id="test",
    image_path="/images/test.jpg",
    tasks=[ProcessingTask.CUSTOM_FILTER]
)
Add Python Worker (for ML tasks)
class MLAnalysisWorker(Agent):
    async def process(self, message: Message) -> Message:
        # Use Python ML libraries (PyTorch, TensorFlow, etc.)
        result = self.ml_model.predict(image)
        return Message(role="agent", content="analyzed", metadata=result)

# Register with orchestrator
orchestrator.add_python_worker(MLAnalysisWorker())

Observability

Distributed Tracing

Both Python and Go workers use OpenTelemetry for tracing:

Request Flow:
  Python Orchestrator (span: orchestrate_job)
    │
    ├── Go Worker 1 (span: process_metadata)
    │     └── extract_exif (span: internal operation)
    │
    ├── Go Worker 2 (span: process_thumbnail)
    │     └── resize_image (span: internal operation)
    │
    └── Go Worker 3 (span: process_optimize)
          └── compress_image (span: internal operation)

View traces in Jaeger UI to see:

  • Cross-language call graph
  • Latency breakdown
  • Error propagation
  • Performance bottlenecks
Metrics

Track key metrics:

# Python orchestrator metrics
- jobs_processed_total
- jobs_processing_duration_seconds
- worker_requests_total
- worker_request_duration_seconds

# Go worker metrics
- tasks_processed_total
- tasks_processing_duration_seconds
- memory_usage_bytes
- goroutines_count

Best Practices

Language Selection

Use Python for:

  • API endpoints and web servers
  • ML model inference (PyTorch, TensorFlow)
  • Data science and analysis
  • Rapid prototyping
  • Glue code and orchestration

Use Go for:

  • CPU-intensive processing
  • High-throughput services
  • Memory-constrained environments
  • Low-latency requirements
  • Systems programming
Communication Patterns

gRPC (recommended for this example):

  • High performance
  • Strong typing
  • Streaming support
  • Language interoperability

HTTP/REST:

  • Simple integration
  • Wider compatibility
  • Browser-friendly

Message Queue:

  • Async processing
  • Fault tolerance
  • Backpressure handling

Benchmarking

Run your own benchmarks:

# Python side only (baseline)
cd python && python3 -m timeit -s "from benchmark import run_python_only" "run_python_only(100)"

# Cross-language system
python3 main.py benchmark

Expected results:

  • Metadata extraction: 15-20x faster in Go
  • Thumbnail generation: 10-15x faster in Go
  • Image optimization: 20-50x faster in Go
  • Overall system: 10-15x faster with Go workers

Production Considerations

  • ✅ Horizontal scaling with multiple Go workers
  • ✅ Load balancing (round-robin, least-connections)
  • ✅ Health checks and auto-recovery
  • ✅ Distributed tracing across languages
  • ✅ Metrics and monitoring
  • ⚠️ Add retry logic with backoff
  • ⚠️ Add circuit breakers
  • ⚠️ Add rate limiting
  • ⚠️ Add authentication/authorization
  • ⚠️ Add request validation
  • patterns/: Individual agent patterns
  • customer-support/: Sequential multi-agent pipeline
  • code-review/: Parallel multi-agent execution
  • llm-optimizer/: Cost optimization strategies

Performance Tips

  1. Batch processing: Process multiple images per request to amortize gRPC overhead
  2. Connection pooling: Reuse gRPC connections
  3. Worker colocation: Deploy workers close to data sources
  4. Caching: Cache metadata, thumbnails, etc.
  5. Compression: Use gRPC compression for large payloads

Built with AgentKit - Production-grade multi-agent framework with cross-language support

Performance: 10-100x improvement for CPU-intensive tasks

Architecture: Best-of-both-worlds - Python flexibility + Go performance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL