Skip to content

mod_logic_python

Germán Luis Aracil Boned edited this page Apr 6, 2026 · 1 revision

mod_logic_python

Category: Logic & Scripting | Version: v1.1.0 | Source: modules/mod_logic_python/mod_logic_python.c

Description

Python 3 scripting engine. Runs in a forked subprocess with JSON pipe bridge. Avoids CPython signal conflicts with libev. Full Python ecosystem available (pip packages).

Constants

#define PY_MAX_ROUTES   128
#define PY_BUF_SIZE     65536

Python API

import portal

# Read a path
resp = portal.get("/health")

# Write a value
portal.set("/cache/session", "token123")

# Call an action
result = portal.call("/dns/resolve", body="example.com")

# Register a route
@portal.route("/myapp/analyze")
def analyze(msg):
    return {"status": 200, "body": "Analysis complete"}

# Subscribe to events
@portal.on("/events/cache/*")
def on_cache_event(event):
    portal.log(f"Cache event: {event[path]}")

# Logging
portal.log("Processing started")

Example Application

# /var/lib/portal/apps/analytics/main.py
import portal
import json

@portal.route("/myapp/report")
def generate_report(msg):
    # Gather data from multiple modules
    metrics = json.loads(portal.get("/metrics/cpu").body)
    devices = json.loads(portal.get("/iot/devices").body)
    cache_keys = json.loads(portal.get("/cache/keys").body)

    report = {
        "cpu_usage": metrics.get("usage", "N/A"),
        "device_count": len(devices),
        "cached_items": len(cache_keys),
    }
    return {"status": 200, "body": json.dumps(report, indent=2)}

Characteristics

  • Subprocess: Forked process, isolated from core
  • JSON bridge: Messages serialized as JSON over pipe
  • Full ecosystem: Use any pip package (numpy, requests, etc.)
  • Safe: Python crash never affects the core
  • Slower: ~1ms per handler call (serialization overhead)

Clone this wiki locally