方法
每张图 Submit + Wait。图像字节提交后即可释放。队列满策略见 InferOverflow。被 DropOldest 挤掉的票 Wait 抛 AiBusyException。
提交与等待
Submit(byte[] imageBytes, in ImageDesc desc)
public ulong Submit(byte[] imageBytes, in ImageDesc desc)
提交一张图。成功返回 ticket(>0)。图像会被拷走,调用方可立即释放入参。可多线程 Submit。
参数:
imageBytes(byte[]) — 同 InferImagedesc(ImageDesc) — 图像描述。C# 绑定无 Gray8 / Rgba
返回值:
ulong— 票号,交给Wait/TryWait
Overflow=Fail 且队列满:立即抛 AiBusyException(DARRA_BUSY=19)。Block 等到有空位。DropOldest 丢掉最旧未开跑任务(它的 Wait 得 AiBusyException)。
示例:
ulong ticket = pool.Submit(File.ReadAllBytes("a.jpg"), ImageDesc.Auto());
string json = pool.Wait(ticket);
SubmitAsync(byte[] imageBytes, ImageDesc desc)
public Task<string> SubmitAsync(
byte[] imageBytes,
ImageDesc desc,
CancellationToken cancellationToken = default)
异步糖:Submit + Wait。图像会被拷走。
参数:
imageBytes(byte[]) — 同Submitdesc(ImageDesc) — 同SubmitcancellationToken(CancellationToken) — 提交前取消
返回值:
Task<string>— 结果 JSON
示例:
string json = await pool.SubmitAsync(jpg, ImageDesc.Auto());
Wait(ulong ticket)
public string Wait(ulong ticket)
等待一张图的结果 JSON。托管副本,无需释放。被 DropOldest 挤掉的票抛 AiBusyException。
参数:
ticket(ulong) —Submit返回的票号
返回值:
string— 结果 JSON,结构同InferImage
示例:
string json = pool.Wait(ticket);
TryWait(ulong ticket)
public string TryWait(ulong ticket)
非阻塞。还没好抛 AiBusyException(message 说明仍在队列 / 推理)。
参数:
ticket(ulong) —Submit返回的票号
返回值:
string— 已完成时的结果 JSON
示例:
try
{
string json = pool.TryWait(ticket);
}
catch (AiBusyException)
{
// 仍在队列或推理
}
资源释放
Dispose()
public void Dispose()
关闭池:拒新任务、等在飞 Infer 结束、关全部会话。重复 Dispose 安全。NULL 安全(已释放再调无操作)。
禁止与 Submit / Wait 并发。Dispose 后句柄失效,再用抛 ObjectDisposedException。
示例:
using var pool = AiInferPool.Open("inspect.darmodel");
InferPoolOptions
同模型推理池选项。MaxWorkers 硬顶 8;GPU 上 yolov8n 建议 1–2。
| 类别 | 属性 | 类型 | 访问 | 说明 |
|---|---|---|---|---|
| InferPoolOptions | MaxWorkers | int | init | 同时打开的会话数。0 = 1。合法 1..8。硬顶 8 |
QueueCapacity | int | init | 未开跑任务上限。0 = 64。合法 1..4096。突发 100 张应 ≥ 100 或用 Block | |
Overflow | InferOverflow | init | 队列满策略。默认 Fail | |
Worker | SessionOptions? | init | 每个 worker 的 CPU 线程 / 亲和 / GPU。null = Core 默认。池会强制 Unique | |
Weight | int | init | 相对份额 0=100。与其它池抢同一 GPU 的并发槽,不是 SM% |
MaxWorkers
public int MaxWorkers { get; init; }
同时打开的会话数。0 = 1。合法 1..8。硬顶 8。GPU yolov8n 建议 1–2;8GB+ 可试 2–4。
默认值: 0(视为 1)
QueueCapacity
public int QueueCapacity { get; init; }
未开跑任务上限。0 = 64。合法 1..4096。突发 100 张应 ≥ 100,或 Overflow = Block。
默认值: 0(视为 64)
Overflow
public InferOverflow Overflow { get; init; }
队列满策略。默认 Fail。
默认值: Fail
相关结构:
public enum InferOverflow
{
Fail = 0, // submit 立即失败(DARRA_BUSY)。工业默认
Block = 1, // submit 等到有空位
DropOldest = 2, // 丢掉最旧未开跑的任务,保最新帧
}
Worker
public SessionOptions? Worker { get; init; }
每个 worker 会话的 CPU 线程 / 亲和 / GPU。null = Core 默认。池会强制 Unique=true,不会跟用户 Open 的驻留实例挤成一份。
默认值: null
禁止改 Windows ReservedCpuSets,禁止动 PLC 隔离核。多卡:每个池钉 Worker.GpuDevice;4 卡 = 4 个池,卡号 0..3。
Weight
public int Weight { get; init; }
相对份额 0=100。与其它池 / 会话抢同一 GPU 的并发槽,不是 CUDA SM%。
默认值: 0(视为 100)
示例:
using var pool = AiInferPool.Open("inspect.darmodel", options: new InferPoolOptions
{
MaxWorkers = 2,
QueueCapacity = 64,
Overflow = InferOverflow.Fail,
Worker = new SessionOptions { GpuDevice = 0, IntraOpThreads = 2 },
Weight = 100,
});
GpuScheduler
同一 GPU 上多个模型按 Weight 分并发推理槽。不是 CUDA SM 百分比。
GpuScheduler.SetGpuSlots()
public static void SetGpuSlots(int gpuDevice, int slots)
设置某张卡同时 InferImage 上限。device 对应 SessionOptions.GpuDevice。
参数:
gpuDevice(int) — GPU 序号。0 = 第一块卡slots(int) — 同时推理上限(张)。0(默认)= 不限制。>0时按 weight 份额排队:某 owner 最多同时跑max(1, slots * weight / 100)路,全局仍不超过slots
slots=0:只有一个模型时不限制;两个及以上模型默认 4 槽,按各池 / 会话 Weight 比例分配。线程安全。
示例:
GpuScheduler.SetGpuSlots(gpuDevice: 0, slots: 4);