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

Cornelis Boon

03/04/2024, 12:55 PM
Hi there, I'm currently trying out flyte and still going over the basics. Does every function have to be a task? What are best practices about sharing code between tasks in Flyte? Thanks!
Based on how small some of the tasks examples are in the documentation, it seems like it. And if then if I want to reuse a task of tasks in a workflow, I would decorate it with @workflow 🤔 and then use that workflow in other workflows.
c

Chandrashekhar Kotekar

03/04/2024, 1:15 PM
re. tasks - not all functions need to be tasks. We can have standard Python functions which are not tasks. Example: def add_2(num: int): '''This is normal Python function''' return num + 2 @task() def my_awesome_task() -> int: result = add_2(10) return result
c

Cornelis Boon

03/04/2024, 1:17 PM
Oh! So what's a guideline on when to annotate something as a task? Or is this personal preference?
c

Chandrashekhar Kotekar

03/04/2024, 1:18 PM
Usual guidelines of software engineering - try to write functions such that they do only one thing, logical separation of concerns, etc. Flyte task can then call one or more functions.
c

Cornelis Boon

03/04/2024, 1:21 PM
Alright. That makes more sense now
Oh, one last question, flyte will magically figure out which local functions/files with functions are associated to a task when I register a task/workflow? So that imports/refs won't break?
c

Chandrashekhar Kotekar

03/04/2024, 2:22 PM
Yes it will
c

Cornelis Boon

03/04/2024, 2:22 PM
Sick. Okay. Now I'm keen to start building and trying it out
c

Chandrashekhar Kotekar

03/04/2024, 2:23 PM
From my limited experience with Flyte I know that Flyte goes through code, figures out relationship between tasks, workflows etc, creates protobuf files for them and registes the workflow. I am not really Flyte dev team member so they can tell more about this whole process.
c

Cornelis Boon

03/04/2024, 2:24 PM
Gotcha! Thanks for explaining this
k

Ketan (kumare3)

03/04/2024, 3:53 PM
It’s not magical- you need to add @task decorator to functions you want to consider
c

Cornelis Boon

03/04/2024, 5:16 PM
Wait. So do I or do I not need to add a @tassk decorator to every function? I even found one example in the documentation where:
This is a plain Python function that will be called in a subsequent Flyte task. This example demonstrates how Flyte tasks are simply entrypoints of execution, which can themselves call other functions and routines that are written in pure Python.
https://docs.flyte.org/en/latest/user_guide/data_types_and_io/flytedirectory.html
d

David Espejo (he/him)

03/04/2024, 11:29 PM
Not necessarily. What the example tries to illustrate is that the decorator is kinda the interface with Flyte's API. Inside a decorated task you could actually invoke non-decorated Python code if necessary