Hi there, I'm currently trying out flyte and still...
# ask-the-community
c
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
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
Oh! So what's a guideline on when to annotate something as a task? Or is this personal preference?
c
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
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
Yes it will
c
Sick. Okay. Now I'm keen to start building and trying it out
c
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
Gotcha! Thanks for explaining this
k
It’s not magical- you need to add @task decorator to functions you want to consider
c
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
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