Is there a way to run functions that are decorated...
# prefect-community
k
Is there a way to run functions that are decorated with @task outside of prefect? I'm trying to rapidly develop individual task stages but I'd prefer to not have two functions defined. Currently, calling a function decorated with @task in a jupyter notebook throws a context error.
c
Hi @Kyle McEntush - absolutely. E.g.,:
Copy code
@task
def my_function(x, y):
    return x + y

my_function.run(1, 2) # runs with no Prefect intervention
👍 1
1
k
Got it, thanks!
👍 1
c
@Marvin archive “How to run a Prefect Task without Prefect”
k
Interesting. Calling .run() on a task that lives within a class doesn't automatically pass the "self" argument.
Is that intentional?
c
Can you share a traceback? Did you initialize the class before calling
.run()
?
k
Copy code
from prefect import task

class Test:
    def __init__(self):
        pass

    @task
    def test_func(self, val):
        return val
    
test = Test()
test.test_func.run(1)
Throws
Copy code
TypeError: test_func() missing 1 required positional argument: 'val'
c
I see - this is not a supported pattern; all arguments to a task need to be capable of being converted to tasks themselves. To achieve the same thing you should do:
Copy code
from prefect import Task

class Test(Task):
    def run(self, val):
        return val

test = Test()
test.run(1)
k
in that setup, can you have multiple functions that can be run?
c
What do you mean?
k
Copy code
from prefect import Task

class Test(Task):
    def run(self, val):
        return val

    def run2(self, val):
        return val

test = Test()
test.run(1)
test.run2.run(1)
oops, second
I presume that won't work for the same reasons as before
c
What are you trying to achieve with this setup?
k
The end goal is to have multiple tasks defined within a class
but it seems that goes against the design of prefect
c
but to what end? Why do the tasks need to all be methods of a single class?
k
many of the tasks share common arguments, so I figured a class would be ideal
But within the current structure I could always pass a dict through to mimic the self arg
c
gotcha; because Prefect supports distributed environments, the only way to share state between tasks is via arguments <-> return values, so yea a dict of data makes sense
k
Okay, thanks for the tip
c
👍 anytime
k
appreciate your responsiveness at 6:30pm on a Friday 🙂
c
haha 🍻