Worker¶
任务单元。继承 BaseWorker 并实现 _execute()。
Worker 必须以「类」注册。
WorkerRegistry用issubclass校验,传入函数或实例会得到TypeError: issubclass() arg 1 must be a class。
zoo_framework.workers.base_worker.BaseWorker ¶
Worker base class.
Configuration is carried uniformly by the _props dict; is_loop /
run_timeout / delay_time are exposed as properties - reading them
MUST NOT require call syntax, and subclasses MUST NOT shadow them with
instance attributes.
源代码位于: zoo_framework/workers/base_worker.py
13 14 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 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 | |
is_loop
property
¶
Whether to keep this Worker across scheduling rounds and re-execute it.
_props is the single source of truth; treated as non-looping when
undeclared.
period
property
¶
Execution period in seconds.
None when undeclared, meaning event-driven (every scheduling round counts as due). The period is declared per Worker and MUST NOT be pinned uniformly by the process or the scheduling model.
phase
property
¶
Period phase offset in seconds; 0.0 when undeclared.
The offset of the first trigger relative to the schedule baseline, used to stagger the trigger moments of multiple periodic Workers.
delay_time
property
¶
Seconds to wait after each execution; treated as no wait when undeclared.
registry_name
property
¶
The suffix-free identity: configured name, or class name when undeclared.
name is this identity plus the instance ordinal. A receiver
filtering results by "which Worker" MUST match on this identity, since
the ordinal is not known when the filter is declared (issue #229).
run ¶
Execute once.
A failure is reported, not left implicit (issue #228): the result
topic is how consumers observe that a Worker finished, so an execution
body raising MUST still produce one result carrying the failure. The
returned result is marked as a failure (error set, success
false, content None) so consumers can tell "failed" apart from "still
running"; the failure MUST NOT be delivered in the shape of a
successful empty result. The error is logged and _on_error /
_on_done still run, exactly as before.
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
WorkerResult |
WorkerResult
|
The result of this execution. On failure the content
is None and |
源代码位于: zoo_framework/workers/base_worker.py
内建 Worker¶
zoo_framework.workers.event_worker.EventWorker ¶
Bases: BaseWorker
Event Worker.
The key constraint of the consume loop: every event taken from the queue MUST have a settled destination - delivered, requeued, or moved to dead-letter. Silent dropping was historically this class's main source of event loss.
Join-timeout semantics (issue #232): the wait for a round's dispatch
results is bounded by the maximum effective timeout across that
round's reactors. A reactor's effective timeout is two-level: its own
declared event_timout (a positive value = legitimately slow) takes
precedence over the global event:timeout; undeclared reactors follow
the global default, so the merged behavior equals the pre-change one.
Only a future still unfinished after its own effective timeout elapses
is reported as unfinished - a declared-slow reactor no longer triggers
the warning every round.
Ordering (issue #230): a channel's FIFO is a delivery guarantee, not
an execution-order guarantee. This consumer pops events in queue order and
submits each one to the shared reactor executor, so calls to the same
reactor overlap and finish in whatever order they actually complete -
the completion order MUST NOT be assumed to follow the enqueue order, and
consumers that need ordered handling MUST serialize on their own side (an
explicit lock or queue). Enabling event:dispatchBatchingEnabled batches
the same (channel, reactor) events of one round into a single executor
task, which does execute them sequentially in dequeue order; it is a
process-wide switch, not a per-topic declaration.
Note: this class MUST NOT be decorated with any class-replacing decorator
(the historical @cage did exactly that and was deleted).
WorkerRegistry.register_class validates the contract with issubclass;
swapping the class for a function would make it raise
TypeError: issubclass() arg 1 must be a class. The singleton and the
instance cache are owned by WorkerRegistry._worker_instances, with no
second mechanism needed - so this class also does not take
@process_scoped: that would move the "one instance per Worker"
ownership away from WorkerRegistry.
源代码位于: zoo_framework/workers/event_worker.py
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
zoo_framework.workers.state_machine_work.StateMachineWorker ¶
Bases: BaseWorker
State machine Worker - manages state machine persistence.
Features: - automatically loads and saves the state machine - thread-safe state machine access - file checksums and backups
源代码位于: zoo_framework/workers/state_machine_work.py
11 12 13 14 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 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 | |
协程 Worker¶
zoo_framework.workers.async_worker.AsyncWorker ¶
Bases: BaseWorker
Async Worker base class.
Features: - native coroutine support - automatic event loop management - both synchronous and asynchronous execution modes
The abstract constraint is enforced by ABCMeta: a subclass that does
not implement async_execute cannot be instantiated.
源代码位于: zoo_framework/workers/async_worker.py
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 | |
async_init
async
¶
Async initialization.
Subclasses may override this to initialize async resources.
async_destroy
async
¶
Async teardown.
Subclasses may override this to clean up async resources.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
timeout
|
float | None
|
the timeout |
None
|
源代码位于: zoo_framework/workers/async_worker.py
async_execute
abstractmethod
async
¶
The async execution method (subclasses MUST implement).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
*args
|
Any
|
positional arguments |
()
|
**kwargs
|
Any
|
keyword arguments |
{}
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The execution result |
源代码位于: zoo_framework/workers/async_worker.py
execute ¶
The synchronous execution entry point.
MUST be called from a thread with no running event loop. Calling it inside an event loop and returning a Task that nobody awaits would silently skip the coroutine, so this refuses explicitly and states the alternative.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
*args
|
Any
|
positional arguments |
()
|
**kwargs
|
Any
|
keyword arguments |
{}
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The execution result |
引发:
| 类型 | 描述 |
|---|---|
RuntimeError
|
called inside a running event loop |
源代码位于: zoo_framework/workers/async_worker.py
run_in_background ¶
Run in the background.
The worker thread is a daemon: unfinished background tasks MUST NOT prevent interpreter exit.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
*args
|
Any
|
positional arguments |
()
|
**kwargs
|
Any
|
keyword arguments |
{}
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
_BackgroundTask |
_BackgroundTask
|
the background task, observable via done() / result() |
源代码位于: zoo_framework/workers/async_worker.py
原生执行 Worker¶
zoo_framework.workers.dual_arm_worker.DualArmWorker ¶
Bases: BaseWorker
以在线自学的路由决策在「原生/Python」双臂间选择执行体.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
native_task_name |
子类 props 声明的原生任务名;None 表示纯 python 臂 (恒定 python,不参与决策也不取锁) |
子类 MUST 实现 :meth:_execute_python(python 臂执行体);声明了原生臂的,
再提供 :meth:_prepare_native_input(到达 NativeAdapter.execute 的
bytes 载荷;默认取 props["input"])。
关闭语义(spec「关闭时零影响」):adaptive:enabled=false(默认)时按
纯 python 臂执行,不进入 bandit 分支、不取锁。
显式拒绝语义(spec「原生臂不可用时显式拒绝」):声明了原生臂但
native:enabled=false 或扩展缺失/握手失败 → 构造期显式报错,MUST NOT
有人一次静默的 python 臂执行。
源代码位于: zoo_framework/workers/dual_arm_worker.py
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 | |
zoo_framework.native.worker.NativeTaskWorker ¶
Bases: BaseWorker
以既有 Worker 形态执行一个原生任务.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
task_name |
要执行的原生任务名(MUST 已在扩展侧注册) |
|
adapter |
注入的原生适配器;缺省为进程级单例 |
props 里除 BaseWorker 的既有键外,本类读取:
task_name(必需;缺省时报错而不是猜)
结果语义:_execute() 返回转换后的 Python 值,跑进 BaseWorker.run()
统一包成 :class:~zoo_framework.workers.WorkerResult(含 worker 名),
运行标识(run_id / session_id)由结算点盖章——本类不碰身份字段。
源代码位于: zoo_framework/native/worker.py
Worker 相关值对象¶
zoo_framework.workers.worker_props.WorkerProps ¶
zoo_framework.workers.worker_result.WorkerResult ¶
The Worker execution result.
All results are delivered to WaiterConstant.WORKER_RESULT_TOPIC;
worker_name identifies the Worker that produced the result, letting
receivers filter by Worker. cls_name keeps the Worker's class name,
letting receivers tell apart different instances of the same class.
A failed execution also produces a result here (issue #228): the
result topic is how consumers observe that a Worker finished, so a Worker
whose execution body raised MUST still leave exactly one observable result
carrying error. Receivers distinguish "failed" from "still running" by
error / success; a failure MUST NOT be delivered in the shape of a
successful empty result.
worker_name carries the instance ordinal (ok -> ok_1), while
registry_name is the suffix-free identity (the configured name, or
the class name when undeclared). Receivers filtering by Worker SHOULD
match on registry_name: the ordinal is not known in advance, so a
filter built from the registration name would never match worker_name
(issue #229).
run_id / session_id are explicit run-identity fields:
receivers can filter results by run or by session from them, and MUST
NOT rely on implicit context (results may be produced in a worker thread,
and worker threads do not inherit the caller's context variables). The
values are stamped by the dispatch side at settlement.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
topic
|
str
|
the delivery topic; empty falls into the unified result topic |
必需 |
content
|
Any
|
the return value of the Worker's execution body |
必需 |
cls_name
|
str
|
the class name of the Worker that produced the result |
必需 |
worker_name
|
str | None
|
the instance name (with ordinal) of the Worker that produced the result |
None
|
run_id
|
str | None
|
the identity of the run that produced the result |
None
|
session_id
|
str | None
|
the identity of the session the result belongs to |
None
|
error
|
str | None
|
the failure reason; None when the execution succeeded. A non-None value marks this result as a failure, and the content is then None (no execution output was produced) |
None
|
registry_name
|
str | None
|
the suffix-free identity of the Worker that produced the result (configured name, or class name when undeclared) |
None
|
源代码位于: zoo_framework/workers/worker_result.py
success
property
¶
Whether the execution succeeded.
The single failure discriminator: None error means success. A
failed execution MUST NOT be presented as a successful empty result,
so receivers MUST check this (or error) rather than the content.
zoo_framework.workers.worker_register.WorkerRegister ¶
The worker registrar.