You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使Sematic函数在云环境中按指定顺序执行?

确保Sematic函数foo优先于bar执行的解决方案

你的代码中foo(1)和bar(1)属于无依赖的独立任务,Sematic会默认并行调度这类任务,所以云环境中可能出现bar先执行的情况。要保证foo始终优先运行,核心是给两个任务建立执行依赖关系,让bar的启动必须等待foo完成。

修改pipeline函数即可实现:

import typing
import sematic

@sematic.func
def foo(x: int) -> int:
    print(f"Hi foo, {x}!")
    return x * 2

@sematic.func
def bar(x: int) -> int:
    print(f"Hi bar, {x}!")
    return x * 3

@sematic.func
def pipeline() -> typing.List[int]:
    # 先执行foo,等待任务完成并获取结果
    foo_output = foo(1)
    # 再执行bar,此时Sematic会确保foo已执行完毕
    bar_output = bar(1)
    return [foo_output, bar_output]

print(pipeline().resolve())

原理说明

Sematic的任务调度逻辑基于依赖追踪:当你在代码中先调用foo(1)并保存其返回值(即使后续bar不使用该值),Sematic会识别出bar的执行依赖foo的完成状态,从而严格按顺序调度两个任务。这种方式不需要修改foo和bar的函数逻辑,仅通过调整pipeline内的调用顺序和依赖关系就能实现需求。

内容的提问来源于stack exchange,提问作者augray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 01:45:33