Skip to content

📦 ThreadFactory: Context-Aware Submit System #8

@Synaptic724

Description

@Synaptic724

🧠 Feature: Context-Aware Task Submission with Thread Zones

Goal:

Allow .submit() to dynamically route tasks based on thread context and user flags like pure_python. This enables hybrid scheduling:

  • Pure Python tasks can be migrated/flexibly scheduled
  • Unsafe or ambiguous tasks are locked to a dedicated thread

✅ Key Components:

  • ThreadContext

    • Thread-local store to track current task info (e.g. pure_python flag)
  • TaskZone

    • Manages scheduling policy (safe thread pool vs locked thread)
  • SmartSubmitter

    • .submit() wrapper that:
      • Reads current thread context
      • Applies defaults and inheritance rules
      • Supports force_detach for safe overrides

🔐 Core Rules

Situation Behavior
No context (external submit) Uses provided pure_python flag (default: False)
Inside pure_python=False thread Inherit locked context unless forced
Inside pure_python=True thread Use fast pool unless user disables
force_detach=True Force route to chosen pool, skip inheritance

🧩 Example Usage

factory.submit(my_task, pure_python=True)  # Can move across threads

factory.submit(blocking_task)              # Locked by default

factory.submit(lightweight, pure_python=True, force_detach=True)  # Override context

💡 Bonus: Validation Hook

Warn if a task is flagged pure_python=True but fails safety checks:

if pure_python and not validate_func_safety(func):
    warn(f"Function {func.__name__} flagged pure but appears unsafe")

🛠 Tasks To Do

  • Implement ThreadContext (thread-local storage)
  • Add SmartSubmitter logic
  • Route submit() calls via context-aware resolver
  • Support force_detach=True override
  • Log task origin and inheritance decisions for diagnostics
  • (Optional) Add validate_func_safety() to detect C-extension calls

This system is a flexible and scalable foundation for ThreadFactory’s intelligent scheduling pipeline.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions