Hi all, very new to Prefect. Question about using ...
# ask-community
m
Hi all, very new to Prefect. Question about using existing functions. I already have a function in an internal package for posting messages on our internal IM, and import that function. If I want to use it as a task what is the best way to do that? Let's say I have a function postMessage(message) is this right?
import prefect
from foo import postMessage
@task
def postNewMessage(message):
postMessage(message = message)
with Flow("My Flow") as flow:
postNewMessage(message = "Hello World")
or is there a way I can just use my existing function as task instead of nesting it?
1
k
Hey @matthew dickinson, yes you can do this:
Copy code
def test(x):
    return x + 1

with Flow("aa") as flow:
    task(test)(1)
I think that is the least you can do because it still needs to be wrapped somehow since that decorator returns a Class object.
🙏 1
m
ah, that makes sense
So, I assume I could even do things such as:
Copy code
def test(x):
    return x + 1

@task
def blah:
    pass

with Flow("aa") as flow:
    blahblah()
    task(test)(x = 1, upstream_tasks=[blah], max_retries = 4)
k
Yes that’s right
m
Awesome! thanks
k
Wait no lol
Copy code
with Flow("aa") as flow:
    blah = blah()
    task(test, max_retries = 4)(x = 1, upstream_tasks=[blah])
👏 2
💯 1
upvote 2
m
ah, of course. Makes sense.
j
This helped me a lot! If this is not a documented example I feel like it should be
k
It’s barely used 😆