BackgroundGenerator
- class lib.multithreading.BackgroundGenerator(generator: Callable, prefetch: int = 1, name: str | None = None, args: tuple | None = None, kwargs: dict[str, T.Any] | None = None)
Bases:
MultiThreadRun a task in the background background and queue data for consumption
- Parameters:
generator (Callable) – The generator to run in the background
prefetch (int) – The number of items to pre-fetch from the generator before blocking (see Notes). Default: 1
name (str | None) – The thread name. if
Nonea unique name is constructed of the form {generator.__name__}_N where N is an incrementing integer. Default:Noneargs (tuple | None) – The argument tuple for generator invocation. Default:
None.kwargs (dict[str, T.Any] | None) – keyword arguments for the generator invocation. Default:
None.
Notes
Putting to the internal queue only blocks if put is called while queue has already reached max size. Therefore this means prefetch is actually 1 more than the parameter supplied (N in the queue, one waiting for insertion)
References
https://stackoverflow.com/questions/7323664/
Attributes Summary
List of thread error values
Trueif a thread has errored, otherwiseFalseThe name of the thread
Methods Summary
Checks for errors in thread and raises them in caller.
Check if all threads have completed
is_alive()Check if any threads are still alive
iterator()Iterate items out of the queue
join()Join the running threads, catching and re-raising any errors
start()Start all the threads for the given method, args and kwargs
Attributes Documentation
- errors
List of thread error values
- Type:
list
- has_error
Trueif a thread has errored, otherwiseFalse
- name
The name of the thread
Methods Documentation
- check_and_raise_error() None
Checks for errors in thread and raises them in caller.
- Raises:
Error – Re-raised error from within the thread
- Return type:
None
- completed() bool
Check if all threads have completed
- Return type:
Trueif all threads have completed otherwiseFalse
- is_alive() bool
Check if any threads are still alive
- Return type:
Trueif any threads are alive.Falseif no threads are alive
- iterator() Generator
Iterate items out of the queue
- Yields:
The items from the generator
- Return type:
Generator
- join() None
Join the running threads, catching and re-raising any errors
Clear the list of threads for class instance re-use
- Return type:
None
- start() None
Start all the threads for the given method, args and kwargs
- Return type:
None
- iterator() Generator
Iterate items out of the queue
- Yields:
The items from the generator
- Return type:
Generator