容器¶
按作用域解析共享实例。不替换类——issubclass / isinstance / 类型检查全部原样可用。
zoo_framework.core.container.container.ScopedContainer ¶
Container resolving shared instances per scope.
Usage::
container = ScopedContainer()
container.register(ChannelManager, scope_kind=ScopeKind.PROCESS,
thread_safety=ThreadSafety.INSTANCE_GUARANTEED)
manager = container.resolve(ChannelManager, Scope.process())
源代码位于: zoo_framework/core/container/container.py
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
register ¶
register(target: type | str, *, scope_kind: str, thread_safety: str | None = None, name: str | None = None, factory: Callable[[], Any] | None = None, instance: Any = None, on_release: Callable[[Any], None] | None = None) -> str
Register an item.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target
|
type | str
|
a class (identity derived from module + qualname) or a
string (in which case |
必需 |
scope_kind
|
str
|
scope kind this item belongs to; see |
必需 |
thread_safety
|
str | None
|
thread-safety ownership declaration; see
|
None
|
name
|
str | None
|
explicit registration name, overriding the class-derived one |
None
|
factory
|
Callable[[], Any] | None
|
custom constructor; defaults to calling |
None
|
instance
|
Any
|
a pre-built instance; accepted for the process scope only (see below) |
None
|
on_release
|
Callable[[Any], None] | None
|
destroy hook called when the instance is released,
signature |
None
|
返回:
| 类型 | 描述 |
|---|---|
str
|
The registration id |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
invalid scope or thread-safety ownership, missing required declaration, duplicate registration, or an instance/scope combination that contradicts itself |
TypeError
|
|
源代码位于: zoo_framework/core/container/container.py
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 | |
resolve ¶
Resolve the instance within the given scope.
The scope handle is required: there is no "process scope if omitted" default. A default handle is exactly what would silently break session isolation (see design D3).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target
|
type | str
|
the class given at registration, or its id (string) |
必需 |
scope
|
Scope
|
scope handle |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
The instance within this scope |
引发:
| 类型 | 描述 |
|---|---|
LookupError
|
the id is not registered |
ValueError
|
the scope handle does not match the registration's
scope; or the thread-safety ownership does not permit this
access (items declared container-serialized MUST be consumed
via |
源代码位于: zoo_framework/core/container/container.py
exclusive ¶
Exclusive access: access to this item is serialized inside the with block.
For items declared ThreadSafety.CONTAINER_SERIALIZED - they do not
lock themselves; the container provides serialization. What is yielded
is still the real instance (not a proxy), so type contracts are
unaffected.
The lock is per-registration and reentrant, so re-entering the same item inside an instance method does not deadlock.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target
|
type | str
|
the class given at registration, or its id |
必需 |
scope
|
Scope
|
scope handle |
必需 |
产生:
| 类型 | 描述 |
|---|---|
Any
|
The instance within this scope |
源代码位于: zoo_framework/core/container/container.py
release ¶
Release every instance within the scope, firing each registration's destroy hook.
Only instances belonging to this handle are released: releasing a session scope does not touch process-scoped items (they are process-wide by definition), and releasing a process-scoped handle does not touch session-scoped items.
Idempotent: from the second call on the scope holds no instances, so no hooks fire and nothing raises. A hook that raises does not block the release of the remaining instances - otherwise one bad hook would leak all the others - the exception is logged.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
scope
|
Scope
|
scope handle |
必需 |
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
The ids of the instances released this call (cache order) |
源代码位于: zoo_framework/core/container/container.py
replace ¶
replace(target: type | str, scope: Scope, *, factory: Callable[[], Any] | None = None, instance: Any = None) -> None
Inject an override for this scope (test seam).
The replaced scope range is the same as the resolution scope range: override keys share the shape of cache keys, so a process-scoped item is replaced at process range (it is the one process-wide instance anyway) and a session-scoped item is replaced per session - exactly "the replacement applies only to the given scope".
The injection takes effect immediately and drops instances already built
for that scope. Destroy hooks do NOT fire: replacement is a test
seam, not the end of a lifecycle; run release(scope) first if hooks
are needed.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target
|
type | str
|
the class given at registration, or its id |
必需 |
scope
|
Scope
|
scope handle |
必需 |
factory
|
Callable[[], Any] | None
|
replacement constructor |
None
|
instance
|
Any
|
replacement instance; mutually exclusive with factory |
None
|
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
neither factory nor instance given, or both given |
LookupError
|
the id is not registered |
源代码位于: zoo_framework/core/container/container.py
reset ¶
Reset the container: clear all overrides and release all built instances.
"Back to initial state" means three things: no overrides, no leftover instances, no single-thread bindings. Registrations themselves are kept - they are the container's configuration, not test-produced state.
Instances go through the same path as release, so declared destroy
hooks fire as usual and failures are logged only.
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
The ids of the released instances (sorted, for easy assertion) |
源代码位于: zoo_framework/core/container/container.py
registered ¶
live_names ¶
The ids of instances currently held in this scope (sorted).
One use is verifying that release actually took effect: it must be empty afterwards. Prototype-scoped items are not cached, so this is always empty for them.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
scope
|
Scope
|
scope handle |
必需 |
返回:
| 类型 | 描述 |
|---|---|
list[str]
|
The registration ids of instances built under this handle |
源代码位于: zoo_framework/core/container/container.py
get_registration ¶
Fetch the registration itself (for diagnostics and migration checks).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target
|
type | str
|
the class given at registration, or its id |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Registration
|
The corresponding |
源代码位于: zoo_framework/core/container/container.py
作用域¶
zoo_framework.core.container.scope.Scope ¶
Scope handle.
The handle is an immutable identity, safe to pass across threads; it does not carry the instance cache - the cache belongs to the container.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
kind |
the scope kind, see |
|
session_id |
the session identity; carried only by a session scope, None otherwise |
源代码位于: zoo_framework/core/container/scope.py
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 | |
cache_key
property
¶
The cache subject of the scope; a prototype scope does not cache and returns None.
process
classmethod
¶
session
classmethod
¶
The session-level scope handle.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
session_id
|
str
|
the session identity, usually taken from
|
必需 |
prototype
classmethod
¶
of
classmethod
¶
Derive a session scope from a run identity.
This is an explicit call: the handle is still passed into the
container by the caller; it only saves the manual extraction of
session_id. When the identity is None it is rejected explicitly,
not falling back to the process level - that would silently merge two
sessions into one.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
identity
|
Any
|
a |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
the identity is None or carries no session identity |
源代码位于: zoo_framework/core/container/scope.py
zoo_framework.core.container.scope.ScopeKind ¶
注册¶
zoo_framework.core.container.registration ¶
A registration.
A registration records "how to build an instance of a type, which scope it belongs to, and its thread-safety ownership". The id is taken as module + qualified name rather than the bare class name (see design D2): the bare name would let two same-named classes defined in different places overwrite each other, silently, when resolving to the wrong object.
The type contract is guarded by this object: registered_type is always
the class provided at registration; the container neither replaces it nor
returns a proxy, so isinstance / issubclass keep working as usual.
Registration ¶
A registration.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
name |
the registration id (unique within the process) |
|
registered_type |
the type given at registration; carries the type contract, may be None (when registered by name) |
|
scope_kind |
which scope kind it belongs to |
|
thread_safety |
the thread-safety ownership declaration |
|
explicit_name |
whether the id was given explicitly (rather than derived from the class) |
|
on_release |
the release hook called when the instance is released;
signature |
源代码位于: zoo_framework/core/container/registration.py
qualified_name ¶
Get the process-unique identity of a type (module + qualified name).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
cls
|
type
|
the class to identify |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
An identity like |
引发:
| 类型 | 描述 |
|---|---|
TypeError
|
the argument is not a class |