mirror of
https://github.com/karma-riuk/crab-webapp.git
synced 2025-07-06 06:28:12 +02:00
implement the queue
This commit is contained in:
@ -6,6 +6,7 @@ from typing import Callable, Optional, Set, Any
|
||||
|
||||
class Status(Enum):
|
||||
CREATED = "created"
|
||||
WAITING = "waiting"
|
||||
PROCESSING = "processing"
|
||||
COMPLETE = "complete"
|
||||
|
||||
@ -65,19 +66,5 @@ class Subject:
|
||||
self.results = results
|
||||
# TODO: maybe save results to disk here?
|
||||
|
||||
def launch_task(self, *args, **kwargs):
|
||||
self.status = Status.PROCESSING
|
||||
t = Thread(
|
||||
target=self.task,
|
||||
args=args,
|
||||
kwargs={
|
||||
**kwargs,
|
||||
"percent_cb": self.notifyPercentage,
|
||||
"complete_cb": self.notifyComplete,
|
||||
},
|
||||
daemon=True,
|
||||
)
|
||||
t.start()
|
||||
|
||||
|
||||
request2status: dict[str, Subject] = {}
|
||||
|
46
src/utils/queue_manager.py
Normal file
46
src/utils/queue_manager.py
Normal file
@ -0,0 +1,46 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from collections import deque
|
||||
from utils.observer import Subject, Status
|
||||
|
||||
|
||||
class QueueManager:
|
||||
"""
|
||||
Manages a queue of Subjects, handling status transitions and allowing position queries:
|
||||
CREATED -> WAITING -> PROCESSING -> COMPLETE
|
||||
"""
|
||||
|
||||
def __init__(self, max_workers: int = 5) -> None:
|
||||
self.executor = ThreadPoolExecutor(max_workers=max_workers)
|
||||
self.wait_queue: deque[str] = deque()
|
||||
|
||||
def submit(self, subject: Subject, *args, **kwargs) -> None:
|
||||
subject.status = Status.WAITING
|
||||
# Add to waiting queue
|
||||
self.wait_queue.append(subject.id)
|
||||
# Schedule the task on the executor
|
||||
self.executor.submit(self._run, subject, *args, **kwargs)
|
||||
|
||||
def get_position(self, subject_id: str) -> int:
|
||||
"""
|
||||
Returns 1-based position in waiting queue, or 0 if not waiting.
|
||||
"""
|
||||
try:
|
||||
# index returns 0-based, so +1
|
||||
return self.wait_queue.index(subject_id) + 1
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
def _run(self, subject: Subject, *args, **kwargs) -> None:
|
||||
# Remove from waiting queue as it's now processing
|
||||
try:
|
||||
self.wait_queue.remove(subject.id)
|
||||
except ValueError:
|
||||
pass
|
||||
subject.status = Status.PROCESSING
|
||||
# Execute the user-defined task synchronously in this worker thread
|
||||
subject.task(
|
||||
*args,
|
||||
percent_cb=subject.notifyPercentage,
|
||||
complete_cb=subject.notifyComplete,
|
||||
**kwargs,
|
||||
)
|
Reference in New Issue
Block a user