worker¶
The Worker module defines work nodes in Automa.
This module contains the base classes of workers, which will be used within Automa. Workers are the basic execution units in Automa, with each work node typically corresponding to a function (which can be synchronous or asynchronous) responsible for executing specific business logic.
Worker ¶
Bases: Serializable
This class is the base class for all workers.
Worker has two methods that may be overridden by the subclass:
-
arun(): This asynchronous method should be implemented when your worker does not require almost immediately scheduling after all its task dependencies are fulfilled, and when overall workflow is not sensitive to the fair sharing of CPU resources between workers. If workers can afford to retain and occupy execution resources for their entire execution duration, and there is no explicit need for fair CPU time-sharing or timely scheduling, you should implementarun()and allow workers to run to completion as cooperative tasks within the event loop. -
run(): This synchronous method should be implemented when either of the following holds:-
a. The automa includes other workers that require timely access to CPU resources (for example, workers that must respond quickly or are sensitive to scheduling latency).
-
b. The current worker itself should be scheduled as soon as all its task dependencies are met, to maintain overall workflow responsiveness. In these cases,
run()enables the framework to offload your worker to a thread pool, ensuring that CPU time is shared fairly among all such workers and the event loop remains responsive.
-
In summary, if you are unsure whether your task require quickly scheduling or not, it is recommended to implement the arun() method. Otherwise, implement the run() ONLY if you are certain that you agree to share CPU time slices with other workers.
Source code in bridgic/core/automa/worker/_worker.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
arun ¶
async The asynchronous method to run the worker.
Source code in bridgic/core/automa/worker/_worker.py
run ¶
The synchronous method to run the worker.
get_input_param_names ¶
Get the names of input parameters of the worker. Use cached result if available in order to improve performance.
This method intelligently detects whether the user has overridden the run method or is using the default arun method, and returns the appropriate parameter signature.
Returns:
| Type | Description |
|---|---|
Dict[_ParameterKind, List[str]] | A dictionary of input parameter names by the kind of the parameter. The key is the kind of the parameter, which is one of five possible values:
|
Source code in bridgic/core/automa/worker/_worker.py
ferry_to ¶
Handoff control flow to the specified worker, passing along any arguments as needed. The specified worker will always start to run asynchronously in the next event loop, regardless of its dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | The key of the worker to run. | required |
args | optional | Positional arguments to be passed. | () |
kwargs | optional | Keyword arguments to be passed. | {} |
Source code in bridgic/core/automa/worker/_worker.py
post_event ¶
post_event(event: Event) -> None
Post an event to the application layer outside the Automa.
The event handler implemented by the application layer will be called in the same thread as the worker (maybe the main thread or a new thread from the thread pool).
Note that post_event can be called in a non-async method or an async method.
The event will be bubbled up to the top-level Automa, where it will be processed by the event handler registered with the event type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event | Event | The event to be posted. | required |
Source code in bridgic/core/automa/worker/_worker.py
request_feedback ¶
Request feedback for the specified event from the application layer outside the Automa. This method blocks the caller until the feedback is received.
Note that post_event should only be called from within a non-async method running in the new thread of the Automa thread pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event | Event | The event to be posted to the event handler implemented by the application layer. | required |
timeout | Optional[float] | A float or int number of seconds to wait for if the feedback is not received. If None, then there is no limit on the wait time. | None |
Returns:
| Type | Description |
|---|---|
Feedback | The feedback received from the application layer. |
Raises:
| Type | Description |
|---|---|
TimeoutError | If the feedback is not received before the timeout. Note that the raised exception is the built-in |
Source code in bridgic/core/automa/worker/_worker.py
request_feedback_async ¶
async Request feedback for the specified event from the application layer outside the Automa. This method blocks the caller until the feedback is received.
The event handler implemented by the application layer will be called in the next event loop, in the main thread.
Note that post_event should only be called from within an asynchronous method running in the main event loop of the top-level Automa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event | Event | The event to be posted to the event handler implemented by the application layer. | required |
timeout | Optional[float] | A float or int number of seconds to wait for if the feedback is not received. If None, then there is no limit on the wait time. | None |
Returns:
| Type | Description |
|---|---|
Feedback | The feedback received from the application layer. |
Raises:
| Type | Description |
|---|---|
TimeoutError | If the feedback is not received before the timeout. Note that the raised exception is the built-in |
Source code in bridgic/core/automa/worker/_worker.py
CallableWorker ¶
Bases: Worker
This class is a worker that wraps a callable object, such as functions or methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_or_method | Optional[Callable] | The callable to be wrapped by the worker. If | None |
Source code in bridgic/core/automa/worker/_callable_worker.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 | |
get_input_param_names ¶
Get the names of input parameters of this callable worker. Use cached result if available in order to improve performance.
Returns:
| Type | Description |
|---|---|
Dict[_ParameterKind, List[str]] | A dictionary of input parameter names by the kind of the parameter. The key is the kind of the parameter, which is one of five possible values:
|