src.fairreckitlib.core.threading.thread_processor
This module contains a processor that handles active threads.
Classes:
ThreadProcessor: class that starts new and stops running threads.
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)
1"""This module contains a processor that handles active threads. 2 3Classes: 4 5 ThreadProcessor: class that starts new and stops running threads. 6 7This program has been developed by students from the bachelor Computer Science at 8Utrecht University within the Software Project course. 9© Copyright Utrecht University (Department of Information and Computing Sciences) 10""" 11 12import time 13from typing import List 14 15from .thread_base import ThreadBase 16 17 18class ThreadProcessor: 19 """Processor for multiple threads (derived from ThreadBase class). 20 21 Keeps track of all threads that are started by this processor. 22 The processor acquires ownership of these aforementioned threads and 23 will dispose of them after they are finished. 24 Additionally, these threads can be stopped as well. 25 26 Public methods: 27 28 get_active_threads 29 get_num_active 30 is_active_thread 31 start 32 stop 33 """ 34 35 def __init__(self): 36 """Construct the ThreadProcessor.""" 37 self.threads = {} 38 39 def get_active_threads(self) -> List[str]: 40 """Get the names of any active threads. 41 42 Returns: 43 a list of thread names that are currently running. 44 """ 45 active_threads = [] 46 47 for thread_name, _ in self.threads.items(): 48 active_threads.append(thread_name) 49 50 return active_threads 51 52 def get_num_active(self) -> int: 53 """Get the number of active threads for this processor. 54 55 Returns: 56 the number of threads. 57 """ 58 return len(self.threads) 59 60 def is_active_thread(self, thread_name: str) -> bool: 61 """Get if the thread with the specified name is active. 62 63 Args: 64 thread_name: the name of the thread to query. 65 66 Returns: 67 whether the thread is handled by the processor. 68 """ 69 return thread_name in self.threads 70 71 def start(self, thread: ThreadBase) -> None: 72 """Start the specified thread. 73 74 The processor takes ownership of the thread and will clean it up 75 after it is done running. 76 This function returns a KeyError when a thread with the same name 77 is already being handled by the processor. 78 79 Args: 80 thread: the thread to start. 81 """ 82 thread_name = thread.get_name() 83 if self.is_active_thread(thread_name): 84 raise KeyError('Thread already active with name:', thread_name) 85 86 self.threads[thread_name] = thread 87 88 thread.start(self.remove) 89 # delay so the thread can start up 90 time.sleep(0.05) 91 92 def stop(self, thread_name: str) -> None: 93 """Stop the thread with the specified name. 94 95 The processor requests the thread to stop running. 96 This function returns a KeyError when a thread with the specified name 97 is not being handled by the processor. 98 99 Args: 100 thread_name: the name of the thread to stop. 101 """ 102 if not self.is_active_thread(thread_name): 103 raise KeyError('Thread not active with name:', thread_name) 104 105 # request the thread to stop, it will remove itself after it is done running 106 self.threads[thread_name].stop() 107 108 def remove(self, thread): 109 """Remove the thread from the processor.""" 110 del self.threads[thread.get_name()]
19class ThreadProcessor: 20 """Processor for multiple threads (derived from ThreadBase class). 21 22 Keeps track of all threads that are started by this processor. 23 The processor acquires ownership of these aforementioned threads and 24 will dispose of them after they are finished. 25 Additionally, these threads can be stopped as well. 26 27 Public methods: 28 29 get_active_threads 30 get_num_active 31 is_active_thread 32 start 33 stop 34 """ 35 36 def __init__(self): 37 """Construct the ThreadProcessor.""" 38 self.threads = {} 39 40 def get_active_threads(self) -> List[str]: 41 """Get the names of any active threads. 42 43 Returns: 44 a list of thread names that are currently running. 45 """ 46 active_threads = [] 47 48 for thread_name, _ in self.threads.items(): 49 active_threads.append(thread_name) 50 51 return active_threads 52 53 def get_num_active(self) -> int: 54 """Get the number of active threads for this processor. 55 56 Returns: 57 the number of threads. 58 """ 59 return len(self.threads) 60 61 def is_active_thread(self, thread_name: str) -> bool: 62 """Get if the thread with the specified name is active. 63 64 Args: 65 thread_name: the name of the thread to query. 66 67 Returns: 68 whether the thread is handled by the processor. 69 """ 70 return thread_name in self.threads 71 72 def start(self, thread: ThreadBase) -> None: 73 """Start the specified thread. 74 75 The processor takes ownership of the thread and will clean it up 76 after it is done running. 77 This function returns a KeyError when a thread with the same name 78 is already being handled by the processor. 79 80 Args: 81 thread: the thread to start. 82 """ 83 thread_name = thread.get_name() 84 if self.is_active_thread(thread_name): 85 raise KeyError('Thread already active with name:', thread_name) 86 87 self.threads[thread_name] = thread 88 89 thread.start(self.remove) 90 # delay so the thread can start up 91 time.sleep(0.05) 92 93 def stop(self, thread_name: str) -> None: 94 """Stop the thread with the specified name. 95 96 The processor requests the thread to stop running. 97 This function returns a KeyError when a thread with the specified name 98 is not being handled by the processor. 99 100 Args: 101 thread_name: the name of the thread to stop. 102 """ 103 if not self.is_active_thread(thread_name): 104 raise KeyError('Thread not active with name:', thread_name) 105 106 # request the thread to stop, it will remove itself after it is done running 107 self.threads[thread_name].stop() 108 109 def remove(self, thread): 110 """Remove the thread from the processor.""" 111 del self.threads[thread.get_name()]
Processor for multiple threads (derived from ThreadBase class).
Keeps track of all threads that are started by this processor. The processor acquires ownership of these aforementioned threads and will dispose of them after they are finished. Additionally, these threads can be stopped as well.
Public methods:
get_active_threads get_num_active is_active_thread start stop
40 def get_active_threads(self) -> List[str]: 41 """Get the names of any active threads. 42 43 Returns: 44 a list of thread names that are currently running. 45 """ 46 active_threads = [] 47 48 for thread_name, _ in self.threads.items(): 49 active_threads.append(thread_name) 50 51 return active_threads
Get the names of any active threads.
Returns: a list of thread names that are currently running.
53 def get_num_active(self) -> int: 54 """Get the number of active threads for this processor. 55 56 Returns: 57 the number of threads. 58 """ 59 return len(self.threads)
Get the number of active threads for this processor.
Returns: the number of threads.
61 def is_active_thread(self, thread_name: str) -> bool: 62 """Get if the thread with the specified name is active. 63 64 Args: 65 thread_name: the name of the thread to query. 66 67 Returns: 68 whether the thread is handled by the processor. 69 """ 70 return thread_name in self.threads
Get if the thread with the specified name is active.
Args: thread_name: the name of the thread to query.
Returns: whether the thread is handled by the processor.
72 def start(self, thread: ThreadBase) -> None: 73 """Start the specified thread. 74 75 The processor takes ownership of the thread and will clean it up 76 after it is done running. 77 This function returns a KeyError when a thread with the same name 78 is already being handled by the processor. 79 80 Args: 81 thread: the thread to start. 82 """ 83 thread_name = thread.get_name() 84 if self.is_active_thread(thread_name): 85 raise KeyError('Thread already active with name:', thread_name) 86 87 self.threads[thread_name] = thread 88 89 thread.start(self.remove) 90 # delay so the thread can start up 91 time.sleep(0.05)
Start the specified thread.
The processor takes ownership of the thread and will clean it up after it is done running. This function returns a KeyError when a thread with the same name is already being handled by the processor.
Args: thread: the thread to start.
93 def stop(self, thread_name: str) -> None: 94 """Stop the thread with the specified name. 95 96 The processor requests the thread to stop running. 97 This function returns a KeyError when a thread with the specified name 98 is not being handled by the processor. 99 100 Args: 101 thread_name: the name of the thread to stop. 102 """ 103 if not self.is_active_thread(thread_name): 104 raise KeyError('Thread not active with name:', thread_name) 105 106 # request the thread to stop, it will remove itself after it is done running 107 self.threads[thread_name].stop()
Stop the thread with the specified name.
The processor requests the thread to stop running. This function returns a KeyError when a thread with the specified name is not being handled by the processor.
Args: thread_name: the name of the thread to stop.