https://flyte.org logo
#ask-the-community
Title
# ask-the-community
l

L godlike

07/31/2023, 2:06 PM
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

Pryce

07/31/2023, 2:25 PM
Hey @L godlike, I'm guessing your task
say_hello
also has the same print statement in it?
l

L godlike

07/31/2023, 2:26 PM
No it doesn’t But thanks a lot for your help !
h

Hank Huang

07/31/2023, 2:35 PM
Same here, you can assume say_hello return a string “hello” It will print the @@ I am in twice
p

Pryce

07/31/2023, 2:42 PM
Would you care to show the code for
say_hello
?
h

Hank Huang

07/31/2023, 3:03 PM
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

Pryce

07/31/2023, 3:04 PM
Ahh okay, and you're executing with
pyflyte run
?
h

Hank Huang

07/31/2023, 3:04 PM
just run it locally.
y

Yicheng Lu

07/31/2023, 3:04 PM
In my understanding: 1. first print: happened because flytekit compiles the workflow to DAG 2. second print: happened at run time.
p

Pryce

07/31/2023, 3:05 PM
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

Yicheng Lu

07/31/2023, 3:05 PM
If you also print the
res
, the first print will be promise, the second print will be flytekit literal.
h

Hank Huang

07/31/2023, 3:12 PM
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

Yicheng Lu

07/31/2023, 3:15 PM
Yes. Do you see that the first type (res) printed is a promise, while the second is actually a literal?
h

Hank Huang

07/31/2023, 3:15 PM
Not really, that’s an another topic, it simply print.
Copy code
@@@ i am in
<class 'str'>
@@@ i am in
<class 'str'>
hello
y

Yicheng Lu

07/31/2023, 3:16 PM
Can you try print(res)?
h

Hank Huang

07/31/2023, 3:17 PM
Copy code
@@@ i am in
hello
@@@ i am in
hello
hello
Not sure if it’s related to the env I run
p

Pryce

07/31/2023, 3:20 PM
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

Hank Huang

07/31/2023, 3:21 PM
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

Yicheng Lu

07/31/2023, 3:22 PM
YES!!!
p

Pryce

07/31/2023, 3:23 PM
Yeah that makes more sense
h

Hank Huang

07/31/2023, 3:23 PM
when we compile, we should get a promise, however, if we get the 2nd at run time => we should get the literal, right?
p

Pryce

07/31/2023, 3:26 PM
Yes - the literal value will be produced at run time instead of the promise
h

Hank Huang

07/31/2023, 3:26 PM
Thanks for your explaination! @Pryce @Yicheng Lu Thanks!
p

Pryce

07/31/2023, 3:27 PM
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

Hank Huang

07/31/2023, 3:28 PM
Thanks for the help!
p

Pryce

07/31/2023, 3:28 PM
No worries!
k

Kevin Su

07/31/2023, 4:00 PM
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

L godlike

08/01/2023, 12:57 AM
Thanks a lot for @Pryce @Yicheng Lu @Hank Huang @Kevin Su your guys help ! I will absorb those knowledge !
4 Views