navigate.controller.thread_pool.SynchronizedThreadPool
- class navigate.controller.thread_pool.SynchronizedThreadPool
Bases:
object
A custom thread pool with synchronization and control features.
This class provides a thread pool for managing threads associated with different resources. It allows registering resources, creating threads, wrapping thread tasks, removing threads, and other thread-related operations with explicit control and synchronization.
Note
This class provides explicit control over thread creation, removal,
and synchronization. - It allows managing threads associated with different resources efficiently. - The clear method moves threads to the toDeleteList for later deletion.
- __init__()
Initialize the SynchronizedThreadPool.
Methods
__init__
()Initialize the SynchronizedThreadPool.
clear
()Clear all the threads in the pool.
createThread
(resourceName, target[, args, ...])Create a thread and add it to the waitlist of the resource.
getRunningThread
(resourceName)Get the running thread of the resource.
globaltrace
(frame, event, arg)Global trace function.
killThreadInList
(resourceName, threadList)Kill all the threads in the threadList.
localtrace
(frame, event, arg)Local trace function.
moveToDelete
(resourceName, taskThread)Move a thread to the toDeleteList.
registerResource
(resourceName)Register a resource to the pool.
removeThread
(resourceName, taskThread)Remove a thread from the waitlist of the resource.
threadTaskWrapping
(resourceName, target, *)Wrap the target function of the thread.
Attributes
The resources of the thread pool.
The toDeleteList of the thread pool.
- clear()
Clear all the threads in the pool.
- createThread(resourceName, target, args=(), kwargs={}, *, callback=None, cbArgs=(), cbKargs={})
Create a thread and add it to the waitlist of the resource.
- Parameters:
resourceName (str) – The name of the resource.
target (callable) – The target function of the thread.
args (tuple, optional) – The arguments of the target function, by default ()
kwargs (dict, optional) – The keyword arguments of the target function, by default {}
callback (callable, optional) – The callback function of the thread, by default None
cbArgs (tuple, optional) – The arguments of the callback function, by default ()
cbKargs (dict, optional) – The keyword arguments of the callback function, by default {}
- Returns:
The created thread.
- Return type:
- getRunningThread(resourceName)
Get the running thread of the resource.
- Parameters:
resourceName (str) – The name of the resource.
- Returns:
The running thread.
- Return type:
- globaltrace(frame, event, arg)
Global trace function.
- Parameters:
frame (frame) – The frame of the thread.
event (str) – The event of the thread.
arg (any) – The argument of the thread.
- Returns:
The local trace function if the event is “call”, otherwise None.
- Return type:
callable
- killThreadInList(resourceName, threadList)
Kill all the threads in the threadList.
- Parameters:
resourceName (str) – The name of the resource.
threadList (dict) – The threadList to kill.
- localtrace(frame, event, arg)
Local trace function.
- Parameters:
frame (frame) – The frame of the thread.
event (str) – The event of the thread.
arg (any) – The argument of the thread.
- Returns:
The local trace function if the event is “exception”, otherwise None.
- Return type:
callable
- moveToDelete(resourceName, taskThread)
Move a thread to the toDeleteList.
- Parameters:
resourceName (str) – The name of the resource.
taskThread (SelfLockThread) – The thread to move.
- registerResource(resourceName)
Register a resource to the pool.
- Parameters:
resourceName (str) – The name of the resource.
- removeThread(resourceName, taskThread)
Remove a thread from the waitlist of the resource.
- Parameters:
resourceName (str) – The name of the resource.
taskThread (SelfLockThread) – The thread to remove.
- Returns:
Whether the thread is removed.
- Return type:
bool
- threadTaskWrapping(resourceName, target, *, callback=None, cbArgs=(), cbKargs={})
Wrap the target function of the thread.
- Parameters:
resourceName (str) – The name of the resource.
target (callable) – The target function of the thread.
callback (callable, optional) – The callback function of the thread, by default None
cbArgs (tuple, optional) – The arguments of the callback function, by default ()
cbKargs (dict, optional) – The keyword arguments of the callback function, by default {}
- Returns:
The wrapped target function.
- Return type:
callable
- resources
The resources of the thread pool.
- Type:
dict
- toDeleteList
The toDeleteList of the thread pool.
- Type:
dict