Skip to content

Cache modules created by import_module in wikispeech-server #44

@HannaLindgren

Description

@HannaLindgren

See feedback from ClaudeAI:

Module code re-executed on every request — wasteful and breaks module-level state

util.import_module() is called on every incoming request, for every textprocessor component and for the synthesis adapter:_

pythonspec = importlib.util.spec_from_file_location(module_name, module_file)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)   # ← re-parses and re-executes the .py file each time

This means the module's Python source is re-read from disk, re-parsed, and re-executed on every request. That's unnecessary CPU and I/O overhead that grows with traffic. More subtly, module-level globals are reset to their initial values on each call, because a fresh module object is created. This is why engine_url and mapper_url in matcha_piper_adapter.py are initialized to None and re-fetched from config every single call — the caching intended by the if engine_url is None guard never actually works across requests.

Fix: Use Python's standard importlib.import_module() (which caches in sys.modules) or cache the result in a dict keyed by module name. Modules that are loaded once at startup don't need to be re-imported on each request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions