-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_manager.py
155 lines (137 loc) · 4.62 KB
/
thread_manager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
This is the main file for the application. It manages the threads and the
timer.
"""
import threading
import configparser
from typing import Tuple, Any
from io_util import output_util
from io_util import input_util
from io_util import screen, print_lock
from my_timer import chapter_timer
class ThreadManager:
"""
Manages multiple threads for the application, including a timer, input, and output handling.
"""
def __init__(self, synthesizer=None, chapters: int = 8, extra_essay: bool = False):
self.initialize_events()
self.load_config()
self.synthesizer = synthesizer
self.chapters = chapters
self.screen = screen
self.extra_essay_flag = extra_essay
self.timer_thread = None
self.output_thread = None
self.input_thread = None
def load_config(self):
"""
Loads configuration settings from a file.
"""
config = configparser.ConfigParser()
config.read('config.ini')
self.warning_time = config.getint(
'Timer', 'warning_time', fallback=300)
self.remaining_time = config.getint(
'Timer', 'chapter_time', fallback=1200)
def get_timer_args(self) -> Tuple[Any, ...]:
"""
Prepares and returns the arguments needed for the timer thread.
"""
return (
self.timer_expired_event,
self.extra_essay_event,
self.pause_not_pressed_event,
self.remaining_time,
self.warning_time,
self.screen,
print_lock,
self.synthesizer
)
def get_output_args(self) -> Tuple[Any, ...]:
"""
Returns the arguments needed for the output thread.
"""
return (
self.timer_expired_event,
self.exit_flag,
self.output_event,
self.synthesizer,
self.pause_not_pressed_event,
self.chapters,
)
def get_input_args(self) -> Tuple[Tuple[Any, ...],]:
"""
Returns the arguments needed for the input thread.
"""
return (
(self.timer_expired_event, self.exit_flag, self.output_event,\
self.pause_not_pressed_event),
)
def initialize_events(self):
"""
Initializes threading events.
"""
self.timer_expired_event = threading.Event()
self.pause_not_pressed_event = threading.Event()
self.output_event = threading.Event()
self.exit_flag = threading.Event()
self.extra_essay_event = threading.Event()
def initialize_threads(self, chapter_num: int):
"""
Initializes threads.
"""
self.timer_thread = threading.Thread(
target=chapter_timer, args=(self.get_timer_args(),))
self.output_thread = threading.Thread(
target=output_util, args=(chapter_num, self.get_output_args(),))
self.input_thread = threading.Thread(
target=input_util, args=self.get_input_args())
def join_threads(self):
"""
Joins all threads.
"""
self.timer_thread.join()
self.output_thread.join()
self.input_thread.join()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.join_threads()
def start_threads(self, chapter_num: int):
"""
Starts all threads.
"""
self.initialize_threads(chapter_num)
self.timer_thread.start()
self.output_thread.start()
self.input_thread.start()
def run_threads(self, chapter_num: int):
"""
Starts threads for a specific chapter and ensures proper management.
"""
try:
self.initialize_for_chapter(chapter_num)
self.start_threads(chapter_num)
self.join_threads()
except Exception as e:
print(f"An error occurred: {e}")
self.cleanup()
def initialize_for_chapter(self, chapter_num: int):
"""
Initializes or resets flags and timers for a new chapter.
"""
self.timer_expired_event.clear()
self.pause_not_pressed_event.set()
if chapter_num is 0 and self.extra_essay_flag:
self.extra_essay_event.set()
def cleanup(self):
"""
Performs necessary cleanup, especially in case of an error.
"""
self.timer_expired_event.set()
if self.timer_thread.is_alive():
self.timer_thread.join()
if self.output_thread.is_alive():
self.output_thread.join()
if self.input_thread.is_alive():
self.input_thread.join()