-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.py
83 lines (67 loc) · 2.41 KB
/
Process.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import random # we need to import random since the assignment said the time was random
from threading import Thread, Lock # for threading
from time import sleep # for time purposes import logging
from Clock import myClock
import logging
class Process(Thread):
def __init__(
self,
manager_obj,
current_processes,
command_obj,
service_times,
S_Time,
process_numbers,
semaphore,
):
super(Process, self).__init__()
# setting the processes characteristics
self.process_status = False
self.process_number = process_numbers
# initialising classes objects
self.thread_manager = manager_obj
self.threadCommand = command_obj
# initialising time characteristics
self.starting_time = S_Time
self.service_time = service_times
self.end_time = self.starting_time + self.service_time
# initialising threading components
self.lock = semaphore
self.active_processes = current_processes
def setFinished(self, isFinished):
self.process_status = isFinished
def run(self):
logger = logging.getLogger(f"{__name__} thread")
logger.info(
f"Clock: {self.starting_time}, Process {self.process_number}: Start"
)
while self.end_time > myClock.time:
self.execute()
logger.info(
f"Clock: {self.end_time}, Process {self.process_number}: Finished"
)
for proces in self.active_processes:
if proces.process_number == self.process_number:
self.active_processes.remove(proces)
break
logger.debug(f"Exit Process {self.process_number} Thread")
def execute(self):
with self.lock:
wait_time = (
min(
self.end_time - myClock.time,
random.randrange(0, 1000),
)
/ 1000
)
if wait_time > 0:
sleep(wait_time)
if self.end_time - myClock.time > 300:
command = self.threadCommand.list[self.threadCommand.index]
self.threadCommand.next_cmd()
self.thread_manager.api(
command,
self.process_number,
)
else:
self.process_status = True