Hi, community I am studying how workflow works and...
# ask-the-community
l
Hi, community I am studying how workflow works and found that
Copy code
@workflow
def my_wf() -> str:
    print("@@@ i am in")
    res = say_hello()
    return res
if I execute it locally,
Copy code
@@@ i am in
@@@ i am in
Will appear in my terminal Can anyone explain why or guide me some reference ? Thanks a lot !
p
Hey @L godlike, I'm guessing your task
say_hello
also has the same print statement in it?
l
No it doesn’t But thanks a lot for your help !
h
Same here, you can assume say_hello return a string “hello” It will print the @@ I am in twice
p
Would you care to show the code for
say_hello
?
h
sure, following is the code.
Copy code
def say_hello():
    return "hello"

@workflow
def my_wf() -> str:
    print("@@@ i am in")
    res = say_hello()
    return res

my_wf()
p
Ahh okay, and you're executing with
pyflyte run
?
h
just run it locally.
y
In my understanding: 1. first print: happened because flytekit compiles the workflow to DAG 2. second print: happened at run time.
p
Yeah I don't think you need to call it explicitly at the bottom of the script. If you remove
my_wf()
it should work as expected
y
If you also print the
res
, the first print will be promise, the second print will be flytekit literal.
h
I see, looks like the compile will also trigger the print function. if I change the code to the following, it will print 5 line, 4 from my_wf, 1 from last line. Does this result meet yours expectation? @Yicheng Lu @Pryce
Copy code
@workflow
def my_wf() -> str:
    print("@@@ i am in")
    res = say_hello()
    print(type(res))
    return res

print(my_wf())
y
Yes. Do you see that the first type (res) printed is a promise, while the second is actually a literal?
h
Not really, that’s an another topic, it simply print.
Copy code
@@@ i am in
<class 'str'>
@@@ i am in
<class 'str'>
hello
y
Can you try print(res)?
h
Copy code
@@@ i am in
hello
@@@ i am in
hello
hello
Not sure if it’s related to the env I run
p
Hmm interesting, I was expecting one of the print(type(res)) to be a promise. I guess it depends how you're executing, if it's just with
python
or
pyflyte run
. But otherwise expected.. if you remove the
()
from
print(my_wf())
you should only get one print loop.
h
my bad
Copy code
@task
def say_hello()->str:
    return "hello"
we should @ task
so that it can be wrapped as a promise
Copy code
@@@ i am in
Promise(node:n0.o0)
@@@ i am in
Resolved(o0=<FlyteLiteral scalar { primitive { string_value: "hello" } }>)
hello
Does these are the expected result? @Yicheng Lu @Pryce
y
YES!!!
p
Yeah that makes more sense
h
when we compile, we should get a promise, however, if we get the 2nd at run time => we should get the literal, right?
p
Yes - the literal value will be produced at run time instead of the promise
h
Thanks for your explaination! @Pryce @Yicheng Lu Thanks!
p
Also checkout the
pyflyte run
vs
python
section here that describes the different ways of running locally, if you haven't seen it yet.. https://docs.flyte.org/projects/cookbook/en/latest/index.html
h
Thanks for the help!
p
No worries!
k
you should not use print in the workflow. @workflow is used to define your DAG. when you run the workflow on flyte cluster, flyte will only run each individual task (say_hello) in the pod, and won’t run workflow function (my_wf). if you want to use print, for loop in the workflow, you can use @dynamic.
l
Thanks a lot for @Pryce @Yicheng Lu @Hank Huang @Kevin Su your guys help ! I will absorb those knowledge !