forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.py
55 lines (48 loc) · 1.97 KB
/
elevator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import time
from threading import Lock, Condition
from request import Request
from direction import Direction
class Elevator:
def __init__(self, id: int, capacity: int):
self.id = id
self.capacity = capacity
self.current_floor = 1
self.current_direction = Direction.UP
self.requests = []
self.lock = Lock()
self.condition = Condition(self.lock)
def add_request(self, request: Request):
with self.lock:
if len(self.requests) < self.capacity:
self.requests.append(request)
print(f"Elevator {self.id} added request: {request.source_floor} to {request.destination_floor}")
self.condition.notify_all()
def get_next_request(self) -> Request:
with self.lock:
while not self.requests:
self.condition.wait()
return self.requests.pop(0)
def process_requests(self):
while True:
with self.lock:
while self.requests:
request = self.get_next_request()
self.process_request(request)
self.condition.wait()
def process_request(self, request: Request):
start_floor = self.current_floor
end_floor = request.destination_floor
if start_floor < end_floor:
self.current_direction = Direction.UP
for i in range(start_floor, end_floor + 1):
self.current_floor = i
print(f"Elevator {self.id} reached floor {self.current_floor}")
time.sleep(1) # Simulating elevator movement
elif start_floor > end_floor:
self.current_direction = Direction.DOWN
for i in range(start_floor, end_floor - 1, -1):
self.current_floor = i
print(f"Elevator {self.id} reached floor {self.current_floor}")
time.sleep(1) # Simulating elevator movement
def run(self):
self.process_requests()