跳到主要内容

打开

打开加密模型池或明文模型池。UI 只在 activate 要一次码;之后 open 免密。Submit / Wait 永不收密码。

optionsNone = workers=1、queue=64、FAIL。max_workers 硬顶 8。指名但不可用 = fail-closed

Windows 无 RKNN

Windows 收到 rknn 载荷抛 UnsupportedPayloadError。Windows 上指名 edge-rknnUnsupportedPlatformError

打开

InferPool.open()

@classmethod
def open(
cls,
container_path: str,
*,
prefer_provider: str | None = None,
options: InferPoolOptions | None = None,
) -> InferPool

打开加密模型池。不收激活码。

参数:

  • container_path (str) — 容器路径,不能为空
  • prefer_provider (str | None) — 指名 Provider;None = 按载荷 × 硬件 × 已装包自动选
  • options (InferPoolOptions | None) — 池选项。None = workers=1、queue=64、FAIL

返回值:

  • InferPool — 已打开的池
备注

本机已绑机,或本进程已经 activate 过:直接开。未激活由 Core 报需要激活,UI 应改走 activate。旧双文件 .darmkeyopen_with_key。路径为空抛 InternalError(码 18)。

示例:

from darra_ai import InferPool, InferOverflow, InferPoolOptions

opt = InferPoolOptions(max_workers=2, queue_capacity=64, overflow=InferOverflow.FAIL)
with InferPool.open("model.darmodel", options=opt) as pool:
ticket = pool.submit(jpg)
print(pool.wait(ticket))

InferPool.activate()

@classmethod
def activate(
cls,
path: str,
activation_code: str,
*,
prefer_provider: str | None = None,
options: InferPoolOptions | None = None,
) -> InferPool

出厂包首次激活后开池:UI 只在这里向用户要一次激活码。

参数:

  • path (str) — 容器路径,不能为空
  • activation_code (str) — 出厂激活码,不能为空
  • prefer_provider (str | None) — 指名 Provider;默认 None
  • options (InferPoolOptions | None) — 池选项;默认 None

返回值:

  • InferPool — 已激活并打开的池
备注

永久激活码:成功后本机再 open 免密。Submit / Wait 永不收密码。试用每次 activate 扣一次;本进程内再 open 不用再填。pathactivation_code 为空抛 InternalError(码 18)。

示例:

with InferPool.activate("model.darmodel", "XXXX-XXXX") as pool:
print(pool.wait(pool.submit(jpg)))

InferPool.open_with_key()

@classmethod
def open_with_key(
cls,
path: str,
key_path: str,
*,
prefer_provider: str | None = None,
options: InferPoolOptions | None = None,
) -> InferPool

旧双文件旁路 .darmkey。新 DARB1 不要用。

参数:

  • path (str) — 容器路径,不能为空
  • key_path (str) — .darmkey 路径,不能为空
  • prefer_provider (str | None) — 指名 Provider;默认 None
  • options (InferPoolOptions | None) — 池选项;默认 None

返回值:

  • InferPool — 已打开的池
备注

pathkey_path 为空抛 InternalError(码 18)。

示例:

with InferPool.open_with_key("model.darmodel", "model.darmkey") as pool:
print(pool.wait(pool.submit(jpg)))

InferPool.open_plain()

@classmethod
def open_plain(
cls,
model_path: str,
*,
prefer_provider: str | None = None,
options: InferPoolOptions | None = None,
) -> InferPool

打开明文模型池。与 open 同一推理路径,只跳过容器解码与验票。

参数:

  • model_path (str) — 明文模型路径,不能为空
  • prefer_provider (str | None) — 指名 Provider;None = 自动选
  • options (InferPoolOptions | None) — 池选项。None = workers=1、queue=64、FAIL

返回值:

  • InferPool — 已打开的池
备注

model_path 可为 .onnx.pdmodel(同目录须有 .pdiparams)、或含二者的目录。.pt / .pth 仍拒 → UnsupportedPayloadError。路径为空抛 InternalError

示例:

with InferPool.open_plain("model.onnx") as pool:
print(pool.wait(pool.submit(jpg)))

生命周期

close()

def close(self) -> None

关闭池:拒新任务、等在飞 Infer 结束、关全部会话。重复 close / 未打开均为 no-op。

示例:

pool = InferPool.open("model.darmodel")
try:
print(pool.wait(pool.submit(jpg)))
finally:
pool.close()
备注

close 后句柄失效,再调 submit / waitInternalError(「池已关闭或尚未打开」)。禁止与 submit / wait 并发。 __del__ 是兜底,不要当主路径。

__enter__ / __exit__

def __enter__(self) -> InferPool
def __exit__(self, *exc: object) -> None

上下文管理器。退出 with 块时自动 close()

示例:

from darra_ai import InferPool

with InferPool.open("model.darmodel") as pool:
ticket = pool.submit(jpg)
print(pool.wait(ticket))