-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_resources.py
52 lines (43 loc) · 1.67 KB
/
shared_resources.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
import logging
import threading
shutdown_flag = threading.Event()
logger = logging.getLogger(__name__)
class ThreadManager:
def __init__(self):
self.threads = []
self.shutdown_requested = False
def add_thread(self, thread):
if thread and isinstance(thread, threading.Thread):
logger.info("Adding thread %s to thread manager", thread.name)
self.threads.append(thread)
def join_all(self):
for thread in self.threads:
logger.info("Joining thread %s", thread.name)
if thread.is_alive():
logger.info("Thread %s is alive, joining...", thread.name)
try:
thread.join()
except KeyboardInterrupt:
# If a KeyboardInterrupt occurs while joining, we set the shutdown flag
shutdown_flag.set()
# And re-raise the exception to handle it in the outer scope
raise
def shutdown(self):
# Ensure that shutdown is only performed once
logger.info("Shutdown requested")
if self.shutdown_requested:
return
shutdown_flag.set()
# Signal all threads to shutdown
shutdown_flag.set()
self.join_all()
self.shutdown_requested = True
# Additional cleanup logic can be added here if necessary
# Clean up the threads list
self.threads = []
shutdown_flag.clear()
def handle_keyboard_interrupt(self):
if not self.shutdown_requested:
logger.info("KeyboardInterrupt detected, shutting down threads...")
self.shutdown()
thread_manager = ThreadManager()