Module: concurrency_utils
QueueProcessor
Bases: Generic[WorkerContext, TaskContext]
A queue with a list of workers that process tasks from the queue. If a worker fails, the task is re-added to the queue, and the worker is evicted. Another worker will pick up the task. This is useful for processing tasks in parallel where some tasks may fail due to network issues or other reasons.
Source code in src/ritual_arweave/concurrency_utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
__init__(evict_exceptions=None, logger=log)
Initialize the QueueProcessor with a queue and a list of workers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
evict_exceptions |
List[Type[Exception]]
|
A list of exceptions upon which |
None
|
logger |
Any
|
The logger to use for logging. |
log
|
Source code in src/ritual_arweave/concurrency_utils.py
add_task(task)
Add a task to the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task |
TaskContext
|
The task to add. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
add_worker(worker)
Add a worker to the list of workers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
worker |
Worker[WorkerContext, TaskContext]
|
The worker to add. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/ritual_arweave/concurrency_utils.py
process()
Start a thread pool with the workers and process the queue.
Source code in src/ritual_arweave/concurrency_utils.py
Worker
Bases: Generic[WorkerContext, TaskContext]
A worker that processes tasks from a queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
WorkerContext
|
The context for the worker. |
required |
work |
Callable[[WorkerContext, TaskContext], None]
|
A function that processes a task. |
required |
evict_hook |
Callable[[WorkerContext], None]
|
A function that is called when the worker is evicted. |
lambda x: None
|
Source code in src/ritual_arweave/concurrency_utils.py
__init__(context, work, evict_hook=lambda x: None)
Initialize the Worker with a context, a work function, and an evict hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
WorkerContext
|
The context for the worker. |
required |
work |
Callable[[WorkerContext, TaskContext], None]
|
A function that processes a task. |
required |
evict_hook |
Callable[[WorkerContext], None]
|
A function that is called when the worker is evicted. |
lambda x: None
|