Best Practice for Running Multiple Class Instances as Interactive Services

Description

I’m looking for guidance on the proper implementation approach for running multiple LightRAG instances as services within Frappe Framework.

Goal

I plan to simultaneously run approximately 10 instances of LightRAG as services and interact with them through a web interface (start, request/response, stop).

Current Understanding

  • I haven’t found documentation on how to run code as a service in Frappe
  • The background jobs documentation appears to only support silent code execution without request/response capabilities

Approach #1: Background Jobs with Loop

  • Launch each instance as a background job with an internal while loop
  • Create two DocTypes:
    1. For monitoring background job status (start/stop)
    2. For creating requests and receiving responses
  • The job would check for requests to its instance, generate responses, and notify the web interface via events when a response is ready
  • Concern: This implementation seems straightforward but requires many workers

Approach #2: Function Caching

  • Cache (redis_cache) the instance initialization functions and responses to requests through simple function calls via frappe.call
  • Concern: My previous experiments with prototyping using Streamlit showed that caching can cause data overlap - despite functions being called with different parameters, the same instance is retrieved from cache
  • I’m concerned Redis might similarly return the same cache regardless of different function parameters

Questions

  1. Which approach is more reliable?
  2. Are there simpler ways to implement this idea?
  3. What’s the recommended pattern for running services with request/response capabilities in Frappe?