Skip to content

Conversation

@DevaRajan8
Copy link

@DevaRajan8 DevaRajan8 commented Jul 20, 2025

This PR introduces an intelligent priority queue system that ensures high-priority prompts are executed first, even when they arrive shortly after lower-priority prompts have been dequeued.
Problem Solved
Current Issue: In the existing implementation, when a LOW priority prompt is dequeued and starts executing, a HIGH priority prompt enqueued immediately after has to wait until the low-priority task completes. This defeats the purpose of having priority levels.
Example Scenario:

User submits LOW priority prompt → Gets dequeued and starts executing
User submits HIGH priority prompt 1 second later → Has to wait for LOW priority to finish
Result: HIGH priority waits unnecessarily, reducing system responsiveness

Solution
Implemented a "wait-before-execute" mechanism that:

Dequeues a prompt but waits n seconds before starting execution
During this wait period, checks if any higher-priority prompts have been enqueued
If a higher-priority prompt is found, re-queues the current prompt and executes the high-priority one instead
Ensures true priority-based execution order

Technical Changes

  1. Enhanced PromptQueue Class (execution.py)
    pythondef peek_top_priority(self):
    """Check the priority of the top queue item without dequeuing"""
    with self.mutex:
    if len(self.queue) > 0:
    return self.queue[0][0] # Return priority value
    return None
  2. Modified Prompt Worker (main.py)

Added configurable wait_time parameter (default: 1 second)
Implemented priority checking after dequeue
Added re-enqueuing logic for lower-priority prompts
Enhanced logging for better visibility

Benefits

True Priority Execution: HIGH priority prompts execute first, regardless of arrival timing
Improved Responsiveness: Critical tasks get immediate attention
Backward Compatible: No breaking changes to existing functionality
Configurable: wait_time can be adjusted based on use case requirements

Example Output
Dequeued prompt xyz123 with priority: LOW
Re-enqueuing prompt xyz123 (priority: LOW) due to higher-priority prompt detected
Dequeued prompt abc456 with priority: HIGH
Starting execution of prompt abc456 with priority: HIGH
Prompt executed in 159.52 seconds
Dequeued prompt xyz123 with priority: LOW
Starting execution of prompt xyz123 with priority: LOW
Testing

Tested with multiple priority levels (URGENT, HIGH, NORMAL, LOW)
Verified re-enqueuing behavior works correctly
Confirmed thread safety with mutex protection
No performance degradation observed in normal operation

Configuration
The wait time is easily configurable:
pythonwait_time = 10.0 # Adjustable based on requirements
Use Cases
Perfect for scenarios where:

Users submit batch jobs with varying priorities
Critical prompts need immediate execution
System responsiveness is crucial
Multiple users share the same ComfyUI instance

Notes

Adds minimal latency (1 second default) to ensure proper priority handling
Does not interrupt already running executions (by design)
Maintains all existing functionality while adding smart prioritization

This enhancement makes ComfyUI_NetDist more suitable for production environments where prompt prioritization is essential for optimal user experience.

@DevaRajan8 DevaRajan8 closed this Jul 20, 2025
@DevaRajan8 DevaRajan8 reopened this Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant