ThreadSet
ThreadSet
¶
ThreadSet(*threads: Thread)
A container for sets of threads.
It provides boilerplate-removing utilities for handling standard library threads.
See Python's threading.Thread
documentation for general information on Python threads.
You can initialize a ThreadSet with any threads you already defined:
class MyCustomThreadSubclass(threading.Thread):
...
threads = ThreadSet(
MyCustomThreadSubclass(...),
threading.Thread(...),
)
threads.start_and_join()
You can create unions of ThreadSets:
readers = ThreadSet(...)
readers.start()
writers = ThreadSet(...)
writers.start()
(readers | writers).join()
You can create a ThreadSet in a function definition, using with_args(),
repeat(), or target:
With ThreadSet.target, function typing
information is kept:
@ThreadSet.target
def reader(thread_id: int, color: str):
...
@ThreadSet.target
def writer(thread_id: int, color: str):
...
threads = ThreadSet(
reader(0, "red"), # your IDE will show the correct types here
reader(1, "green"),
writer(2, "blue")
)
threads.start_and_join()
Methods:
-
with_args–```python
-
repeat–```python
-
target–```python
-
start–Start the threads in this
ThreadSet. -
join–Join the threads in this
ThreadSet. -
start_and_join–Start the threads in this
ThreadSet, then join them. -
is_alive–Call
Thread.is_alive() -
any_is_alive–```python
-
all_are_alive–```python
-
all_are_not_alive–```python
-
__or__–Returns a new
ThreadSetcontaining all the threads in the operands, -
__ior__–Adds the threads in the right operand into this
ThreadSet. -
__len__–Returns the number of threads contained in this
ThreadSet.