Skip to content

Commit

Permalink
Update 19_8_timer_class.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaltz committed Aug 9, 2023
1 parent 85c1ee6 commit 8ffe800
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions epi_judge_python/19_8_timer_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,70 @@
we need to lock it. The simplest solution is to have a single lock that is
used for all read and writes into the min-heap and the hash table.
"""

from enum import Enum
from heapq import heappush
from time import time
from threading import Thread

NOW = time()


class TaskStatus(Enum):
WAITING = 1
STARTED = 2
CANCELED = 3


class Task(Thread):
def __init__(self, name: str, start_time: float):
super().__init__()
self.name = name
self.start_time = start_time
self.status = TaskStatus.WAITING

def __lt__(self, other: 'Task') -> bool:
return self.start_time < other.start_time

def run(self) -> None:
print(f'Task {self.name} with start time: {self.start_time} started '
f'at {time()}')
self.status = TaskStatus.STARTED


class Timer:
def __init__(self):
self._min_heap = [] # Stores tasks
self._mapping = {} # Stores <task name, task> items

def cancel_task(self, task_name: str):
if task_name not in self._mapping:
raise Exception(f'Task: {task_name} not found')
task = self._mapping[task_name]
if task.status == TaskStatus.STARTED:
print(f'Task: {task_name} has already started. Ignoring the cancel '
f'request.')
elif task.status == TaskStatus.CANCELED:
print(f'Task: {task_name} has already been canceled.')
else: # if task.status == TaskStatus.WAITING:
print(f'Canceling Task: {task_name}.')
task.status = TaskStatus.CANCELED
self._mapping[task_name] = task

def add_task(self, task: Task):
if task.name in self._mapping:
raise Exception(f'Task: {task.name} is already present.')
heappush(self._min_heap, task)
self._mapping[task.name] = task


class Dispatcher(Thread):
def __init__(self, timer: Timer):
super().__init__()
self._timer = timer

def run(self) -> None:
pass


# TODO: To be completed yet!

0 comments on commit 8ffe800

Please sign in to comment.