Skip to main content
The InProcessTaskApp utility enables running task apps entirely within your Python script, eliminating the need for separate terminal processes or manual tunnel management. It automatically starts a FastAPI server, opens a tunnel (SynthTunnel by default, Cloudflare optional), and provides the tunnel URL for GEPA optimization jobs.
In-process task apps are designed for production workflows that run GEPA or eval on the fly, where you want the task app lifecycle to be managed inside the same service or worker that triggers the job.

Quick Start

Features

Automatic Server Management

Starts uvicorn server in background thread

Automatic Tunnel Creation

Opens SynthTunnel by default (Cloudflare optional)

Port Conflict Handling

Automatically finds available ports if requested port is busy

Signal Handling

Graceful shutdown on SIGINT/SIGTERM

Observability

Structured logging and optional callbacks

Multiple Input Methods

Supports app, config, config_factory, or file path

API Reference

InProcessTaskApp

Context manager for running task apps in-process with automatic tunneling.

Parameters

Attributes

  • url (Optional[str]): The public tunnel URL (available after __aenter__)
  • port (int): The actual port the server is running on (may differ from requested if auto_find_port=True)
  • host (str): The host the server is bound to
  • tunnel_mode (str): The tunnel mode being used
  • task_app_worker_token (Optional[str]): SynthTunnel worker token (only set for SynthTunnel)

Raises

  • ValueError: If multiple or no input methods provided, or invalid parameters
  • FileNotFoundError: If task_app_path doesn’t exist
  • RuntimeError: If health check fails or port conflicts can’t be resolved

Usage Examples

Exactly one of app, config, config_factory, or task_app_path must be provided.

1. Direct FastAPI App (app)

2. TaskAppConfig Object (config)

4. File Path (task_app_path)

With Callbacks and Custom Port

Full GEPA Integration

If you’re using SynthTunnel, ensure the job is configured with task_app_worker_token (the SDK can wire this automatically when using run_in_process_job).

Port Conflict Handling

The utility automatically handles port conflicts:
  • auto_find_port=True (default): If requested port is busy, automatically finds next available port
  • auto_find_port=False: Attempts to kill process on port, then raises error if still busy

Input Validation

The utility validates all inputs with clear error messages:
  • Port: Must be in range [1024, 65535]
  • Host: Must be 127.0.0.1, localhost, or 0.0.0.0 (security requirement)
  • Tunnel Mode: "synthtunnel", "quick", "named", "local", or "preconfigured"
  • Task App Path: Must exist and be a .py file
  • Input Methods: Exactly one of app, config, config_factory, or task_app_path must be provided

Logging

The utility uses Python’s logging module:
Log levels:
  • INFO: Major lifecycle events (start, stop, tunnel URL)
  • DEBUG: Detailed operations (port checks, health checks)
  • WARNING: Port conflicts, callback exceptions

Signal Handling

The utility automatically handles SIGINT/SIGTERM signals for graceful shutdown:
  • All active instances are cleaned up on signal
  • Prevents orphaned processes
  • Works seamlessly with context manager cleanup

Requirements

  • Python >= 3.11
  • SynthTunnel (default) requires SYNTH_API_KEY
  • cloudflared binary only if using Cloudflare tunnels (tunnel_backend="cloudflare_quick")
  • Task app must expose /health endpoint
  • Task app must accept X-API-Key header for authentication

Troubleshooting

Port Already in Use

If you see “address already in use” errors:

Health Check Timeout

If health check times out:
  1. Verify task app has /health endpoint
  2. Verify task app accepts X-API-Key header
  3. Increase timeout: health_check_timeout=60.0

Tunnel Not Opening

If tunnel fails to open:
  1. If using SynthTunnel, confirm SYNTH_API_KEY is set
  2. If using Cloudflare, verify cloudflared is installed: which cloudflared
  3. Check network connectivity
  4. Review logs for detailed error messages