luminal.managers.threads
Module Contents
Classes
Custom thread object that can be halted using the |
|
A class for managing multiple threads in a codebase. |
- class TracedThread(*args, **keywords)
Bases:
threading.ThreadCustom thread object that can be halted using the
halt()function.This thread type extends the functionality of the standard
threading.Threadclass to allow for the thread execution to be halted using thehalt()function. Once halted, the thread will immediately stop execution and expose any underlying pipes. This can be useful for debugging or other similar tasks.Important
This thread type is best utilized for small tasks that can be daemonized or terminated quickly.
- __run()
The control function for the
TracedThreadthat sets the global trace function and executes the thread’s main function.Notes
This method is used to run the code in the thread, but with a global trace function set beforehand.
The
globaltraceattribute, set previously by the user, is set as the current trace function at runtime.The previous
__run_backup()method, i.e. the original hardcoded run method, is called here to execute the thread’s main function.The second to last line overwrites the
run()method with the original method, effectively undoing any trace function set previously.
- Return type:
None
- start()
Starts the thread execution by invoking the
Thread.start()method, but not before setting the appropriate run method.Notes
This method overrides the
run()method and replaces it with the__run()method, which sets the global trace function and executes the thread’s main function.The original run method is saved into the
__run_backupattribute before it is swapped out.Finally, the
threading.Thread.start()method is called on the thread object itself to actually start the execution of the code.
- Return type:
None
- globaltrace(frame, event, arg)
The global trace function for a
TracedThreadthat replaces the default function.- Parameters:
frame (
Any) – The current thread frame.event (
Any) – The event type (e.g. ‘call’, ‘line’, ‘return’, ‘exception’).arg (
Any) – Event-specific arguments.
- Returns:
If the event type is
call,self.localtraceis returned, otherwiseNoneis returned.- Return type:
Optional[
self.localtrace]
Notes
This method serves as the global trace function for the
TracedThreadclass.If the event type is
call, the trace function switches toself.localtracewhich handles the local tracing inside the thread in a context-specific manner.If another event type is detected,
Noneis returned as there’s no further action required.
- localtrace(frame, event, arg)
Trace function for the
TracedThreadclass.- Parameters:
frame (
types.FrameType) – The current frame being executed in the thread.event (
str) – The event that occurred in the thread during tracing.arg (
Any) – Optional argument associated with the event.
- Returns:
The next trace function to be called or
Noneto stop tracing.- Return type:
Optional[
Callable]- Raises:
SystemExit: – If the thread is halted and the event is line, the program is forcefully terminated.
Notes
This function can be registered as a trace function on a thread using
sys.settrace(). It is called every time an event is encountered in the traced thread and returns the next trace function to be called. If the thread is halted and the event is line, it raises aSystemExitexception to stop the thread execution.
- halt()
Halt function for the
TracedThreadclass.Notes
This function sets the
haltedattribute of theTracedThreadinstance toTrue, indicating that the thread should be paused for debugging or other purposes. Once halted, the thread will stop executing at the next available opportunity in its trace function.This function is intended to be used in combination with the
localtrace()andglobaltrace()methods from thesysmodule, where the former is registered as a trace function for a thread to halt the thread at a specific point, and the latter is used to globally manipulate the trace function of all threads in the running Python process.
- Return type:
None
- class ThreadManager
A class for managing multiple threads in a codebase.
This class provides functionality for managing and controlling the execution of multiple threads in a program. It maintains a list of running and requested threads, a flag to stop all threads gracefully, and settings for running threads based on given limits.
- _generate_uid(delimiter='-')
Generates a unique identifier for the instance of the
ThreadManagerclass.- Parameters:
delimiter (
str) – The delimiter to use for concatenating the string chunks. Defaults to the - character.- Returns:
A unique identifier for the
ThreadManagerinstance.- Return type:
Notes
This function generates a unique identifier for the
ThreadManagerinstance, based on a randomly generated alphanumeric string. However, it splits the string into six equal chunks and concatenates them with the delimiter to create the final UID.This function is primarily intended for internal use by the
ThreadManagerclass to generate a unique identifier for each instance. It can be called by other methods within the class to ensure that the identifier is unique and consistent for each instance.Note that the delimiter argument can be customized to fit the specific needs of the program or application, but it is recommended that a standard delimiter such as
-or_be used to ensure portability and compatibility with other systems.
- _clean_threads()
Cleans up the list of running threads in the
ThreadManagerinstance.This function removes completed threads from the
_running_threadscollection of aThreadManagerinstance. It does so by creating a copy of_running_threads, looping through it to find completed threads and clears them from the original dictionary.Notes
This function is intended for internal use by the
ThreadManagerclass to clean up the list of running threads after they have finished executing. It should be called regularly to prevent the running thread list from growing too large and potentially causing memory issues.The function works by creating a shallow copy of the running threads dictionary to ensure that the original dictionary is not modified during iteration, which can cause unexpected behavior. If a thread is found to have finished executing, it is removed from the running threads dictionary to keep the list up to date.
Note that this function does not stop or halt any individual threads; it simply removes finished threads from the list of running threads. Any necessary cleanup or stopping of threads should be handled by other methods or functions within the
ThreadManagerclass.
- Return type:
None
- _stop_threads()
Stops all running threads in the
ThreadManagerinstance.This function iterates over the list of running threads in the
ThreadManagerinstance and stops each thread using thehalt()function of theTracedThreadclass. It then waits for each stopped thread to complete using thejoin()function before cleaning up the list of running threads.Important
Invoking the
halt()function on aTracedThreadinstance may cause the thread to stop executing immediately, so it should be used with caution. Additionally, any necessary cleanup or stopping of threads should be handled by other methods or functions within theThreadManagerclass.Notes
This function is intended for internal use by the
ThreadManagerclass to stop all running threads in a program abrubtly. It should be called when the user requests to stop all threads, or when the program is about to exit.The function works by creating a shallow copy of the running threads dictionary to ensure that the original dictionary is not modified during iteration, which can cause unexpected behavior. For each running thread, the function checks if it is still alive, and if so, halts the thread using the
halt()function of theTracedThreadclass. It then waits for the thread to complete halting using thejoin()function before cleaning up the list of running threads.
- Return type:
None
- _start_threads(watching_threads=False)
Starts threads requested in the queue for the
ThreadManagerobject.- Parameters:
watching_threads (Optional[
bool]) – WhenTrue, the method will check every so often to see if any new threads have been appended to the queue. Defaults toFalse.- Raises:
NoThreadsFoundError: – Raised when there are no threads ready to run in the queue.
ThreadLimitReachedError: – Raised when the maximum number of threads allowed to run in the calling
ThreadManagerinstance has been reached.
- Return type:
None
Notes
The
_requested_threadscollection is used to keep track of which threads in the queue need to be executed. The method checks to see if any of the requested threads can be run when the thread limit of theThreadManageris not exceeded. Thread limit is determined by thethread_limitof the callingThreadManagerinstance.If there are no threads in the queue, a
NoThreadsFoundErroris raised. If the maximum number of threads allowed to run in the calling instance has been reached, aThreadLimitReachedErroris raised. Once a thread has been started, it is added to the_running_threadsattribute so it can be tracked.If a thread has already been registered as running but is still in the queue, it is removed so that it doesn’t get executed twice.
- append_thread(function, args=(), kwargs={})
Appends a thread to the request queue for the
ThreadManagerobject.- Parameters:
function (
object) – The callable function to be executed.args (Optional[
tuple]) – A tuple of positional arguments to be passed into the function when executed. Defaults to ().kwargs (Optional(
dict[str, Any]]) – A dictionary of keyword arguments to be passed into the function when executed. Defaults to {}.
- Returns:
True if the thread was successfully appended to the queue, False otherwise.
- Return type:
Notes
The
_requested_threadscollection is used to keep track of which threads can be executed when thread execution is started in theThreadManagerinstance. Each thread is represented as a tuple that containsfunction,argsandkwargsattributes, respectively. Thefunctionis the callable object that will be executed when the thread is run, while theargsandkwargsare arguments that will be passed into thefunctiononce executed.The method returns
Trueif the thread was added to the queue successfully andFalseotherwise. An exception catch is implemented in case there is an error in creating the thread. If the_generate_uid()method raises an exception,Falseis returned as the thread could not be added to the queue.
- stop()
Sets the _flag_request attribute to
True, a signal that stops all running threads in theThreadManagerobject.Notes
The
_flag_requestattribute is a boolean flag that is used to indicate if there is a stop request from theThreadManagerobject. When_flag_requestis set toTrue, all running threads are stopped. This method sets the value of this attribute toTrueand does not return any value. Once this is done, thestop_listening()method is called by theThreadManagerobject so that all the threads that are currently running can terminate.- Return type:
None
- halt()
Halts all running threads in the
ThreadManagerobject.Notes
This method is used to stop (halt) all running threads in the
ThreadManagerobject instance. The method achieves this by calling the_stop_threads()method of theThreadManagerobject which terminates all running threads. After this method is called, the_running_threadscollection of theThreadManageris set to an empty dictionary. The method does not return anything.- Return type:
None
- run()
Starts all threads in the queue for the
ThreadManagerinstance.- Raises:
ThreadsAlreadyRunningError – Raised when new threads have already been started and are currently running.
- Return type:
None
Notes
This method is used to start all threads that are in the
_requested_threadscollection of theThreadManagerobject instance. The method calls the_start_threads()function of theThreadManagerobject, and attempts to start all pending threads in the queue. The function raises aThreadsAlreadyRunningErrorif new threads have already been started and are currently running to avoid starting new threads on top of already running threads.
- watch()
Starts a background thread that continuously starts all threads in the request queue for the
ThreadManagerinstance.- Raises:
ThreadManagerAlreadyRunningError – Raised when a manager has already been spawned and is currently watching for threads to start.
- Return type:
None
Notes
This method starts a parent background thread that continuously tries to start child threads that are in the
_requested_threadscollection of theThreadManagerobject. The method raises aThreadManagerAlreadyRunningErrorif a manager has already been spawned and is currently watching for threads to start. It avoids starting new threads on top of already running threads too.The
_watch()function tries to start any pending threads by calling_start_threads(watching_threads=True)().If the thread limit is reached, a
ThreadLimitReachedErrorexception is caught and then the_clean_threads()method is called to cleanup/shutdown any inactive threads. If any other exception occurs, it will be raised except if the exception message starts with"Thread limit". In this case,_clean_threads()is called to cleanup inactive threads.
In conclusion, the method adds the new thread that it starts to the
_running_threadsattribute of theThreadManagerobject, as well as to the_requested_threadscollection. The method also sets the_currently_watchingflag toTrue. This will prevent subsequent calls to this method if_currently_watchingis alreadyTrue.