m

    matthew dickinson

    10 months ago
    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?
    Kevin Kho

    Kevin Kho

    10 months ago
    Hey @matthew dickinson, yes you can do this:
    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.
    m

    matthew dickinson

    10 months ago
    ah, that makes sense
    So, I assume I could even do things such as:
    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)
    Kevin Kho

    Kevin Kho

    10 months ago
    Yes that’s right
    m

    matthew dickinson

    10 months ago
    Awesome! thanks
    Kevin Kho

    Kevin Kho

    10 months ago
    Wait no lol
    with Flow("aa") as flow:
        blah = blah()
        task(test, max_retries = 4)(x = 1, upstream_tasks=[blah])
    m

    matthew dickinson

    10 months ago
    ah, of course. Makes sense.
    Justin C

    Justin C

    8 months ago
    This helped me a lot! If this is not a documented example I feel like it should be
    Kevin Kho

    Kevin Kho

    8 months ago
    It’s barely used 😆