Thread Management
ManagedThread
dataclass
Encapsulates a thread with crash handling and optional restart logic.
crashed()
Check if the thread has crashed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the thread has crashed, False otherwise. |
join(timeout=None)
Join the thread, waiting for it to finish.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum time to wait for the thread to finish. If None, wait indefinitely. |
None
|
start()
Start the thread if it's not already running.
stop()
Signal the thread to stop cooperatively.
ThreadManager
Manages multiple threads with cooperative stopping and crash handling.
add(name, target, *, args=(), kwargs=None, restart=None, stop_event=None)
Add a new managed thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the thread. |
required |
target
|
Callable[..., Any]
|
The target function to run in the thread. |
required |
args
|
tuple[Any, ...]
|
Positional arguments for the target function. |
()
|
kwargs
|
dict[str, Any]
|
Keyword arguments for the target function. |
None
|
restart
|
RestartPolicy | None
|
Restart policy for the thread. |
None
|
stop_event
|
Event | None
|
Event to signal the thread to stop. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ManagedThread |
ManagedThread
|
The managed thread instance. |
any_crashed()
Check if any managed thread has crashed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if any thread has crashed, False otherwise. |
join_all(timeout_per_thread=5.0)
Join all threads, waiting for them to finish.
start_all()
Start all managed threads.
stop_all()
Signal all threads to stop cooperatively.