Implementation:AUTOMATIC1111 Stable diffusion webui FIFO Lock
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Threading |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Implements a first-in-first-out (FIFO) mutual exclusion lock that guarantees threads acquire the lock in the order they request it.
Description
The FIFO Lock module provides a FIFOLock class that ensures fair ordering of lock acquisition among waiting threads. Unlike a standard threading.Lock, which does not guarantee the order in which waiting threads are woken, this implementation uses a deque of threading.Event objects to track pending threads. When a thread attempts to acquire the lock and it is already held, the thread appends an event to the pending queue and waits on it. When the lock is released, the first pending event is signaled, allowing the longest-waiting thread to proceed next. The class supports Python's context manager protocol (__enter__ and __exit__), making it suitable for use in with statements.
Usage
Use this lock when you need to guarantee that concurrent requests (such as Gradio UI calls to the GPU) are processed in the order they were received, preventing starvation of earlier requests.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/fifo_lock.py
- Lines: 1-37
Signature
class FIFOLock:
def __init__(self) -> None
def acquire(self, blocking=True) -> bool
def release(self) -> None
def __enter__(self) -> bool
def __exit__(self, t, v, tb) -> None
Import
from modules.fifo_lock import FIFOLock
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| blocking | bool | No | If True (default), blocks until the lock can be acquired; if False, returns immediately with False if the lock is unavailable |
Outputs
| Name | Type | Description |
|---|---|---|
| acquired | bool | True if the lock was successfully acquired, False otherwise (only relevant when blocking=False) |
Usage Examples
from modules.fifo_lock import FIFOLock
lock = FIFOLock()
# As a context manager
with lock:
# Critical section - threads enter here in FIFO order
perform_gpu_operation()
# Manual acquire/release
if lock.acquire(blocking=False):
try:
perform_operation()
finally:
lock.release()