简体   繁体   中英

How can I make my Sematic functions execute in a particular order?

I'm using https://sematic.dev , and I have a couple of Sematic functions that I want to execute in a particular order, let's call them foo and bar :

@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]:
    return [foo(1), bar(1)]

print(pipeline().resolve())

But when I run this in the cloud, sometimes bar runs first and I always want foo to run first. How can I do that?

In general, if you're asking this question, it usually means that there's actually a data dependency between the thing you want to run first and the thing you want to run second. In your case, there is probably some data from foo that bar is using. This data may be in some external system like a database, which might be why you're not already representing it in your Sematic functions as inputs and outputs. The best solution is to make this data dependency explicit, having foo return a reference to the data used by bar and passing that reference in explicitly.

However, if for some reason it's very difficult to do this, you can always create a dependency between two functions by feeding the output of one function to the input of another (and then just ignoring it in the second function). In your case that would look something like this:

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

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


@sematic.func
def pipeline() -> typing.List[int]:
    foo_result = foo(1)
    bar_result = bar(1, foo_result)
    return [foo_result, bar_result]

print(pipeline().resolve())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM