Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace threading.Lock() to FIFOLock #12707

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modules/call_queue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from functools import wraps
import html
import threading
import time

from modules import shared, progress, errors, devices
from modules import shared, progress, errors, devices, fifo_lock

queue_lock = threading.Lock()
queue_lock = fifo_lock.FIFOLock()


def wrap_queued_call(func):
Expand Down
37 changes: 37 additions & 0 deletions modules/fifo_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import threading
import collections


# reference: https://gist.github.com/vitaliyp/6d54dd76ca2c3cdfc1149d33007dc34a
class FIFOLock(object):
def __init__(self):
self._lock = threading.Lock()
self._inner_lock = threading.Lock()
self._pending_threads = collections.deque()

def acquire(self, blocking=True):
with self._inner_lock:
lock_acquired = self._lock.acquire(False)
if lock_acquired:
return True
elif not blocking:
return False

release_event = threading.Event()
self._pending_threads.append(release_event)

release_event.wait()
return self._lock.acquire()

def release(self):
with self._inner_lock:
if self._pending_threads:
release_event = self._pending_threads.popleft()
release_event.set()

self._lock.release()

__enter__ = acquire

def __exit__(self, t, v, tb):
self.release()
7 changes: 6 additions & 1 deletion modules/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def progressapi(req: ProgressRequest):
completed = req.id_task in finished_tasks

if not active:
return ProgressResponse(active=active, queued=queued, completed=completed, id_live_preview=-1, textinfo="In queue..." if queued else "Waiting...")
textinfo = "Waiting..."
if queued:
sorted_queued = sorted(pending_tasks.keys(), key=lambda x: pending_tasks[x])
queue_index = sorted_queued.index(req.id_task)
textinfo = "In queue: {}/{}".format(queue_index + 1, len(sorted_queued))
return ProgressResponse(active=active, queued=queued, completed=completed, id_live_preview=-1, textinfo=textinfo)

progress = 0

Expand Down