Can I decorate a <modal.com> function in some way ...
# ask-community
t
Can I decorate a modal.com function in some way and expect Prefect to track it? I saw that I could create a modal task runner, but that applies to whole flows rather than individual tasks. I already have functions I use to fan out data transformations in Modal, and am new to Prefect, but didn’t know if anyone was using Modal this way or if the only “blessed” way was to use the modal worker pool.
n
hi @Taylor Brown - you can run "prefect-decorated" functions just like any python functions and prefect will track those (you don't need deployments / work pools etc) for example, in aws lambda you could do
Copy code
@flow
def handler(event, context): ...
and you'd still get all the run metadata, and events (that you can create automations on) in the UI, as long as you have •
PREFECT_API_URL
PREFECT_API_KEY
(if using cloud) these env vars set in the place where you're running your functions
likewise you don't need deployments to use concurrency features
Copy code
@flow
def some_flow():
   some_task.map(some_iterable).result()
t
Thank you for the fast reply. That’s awesome and one of the things I like about it from what I’ve read. But what about a modal function like: @app.function(image=my_modal_image_for_this_function, gpu=“a100”) @task def process_data(arr : list[str]) -> list[str]: … I would run this in modal as “process_data.remote(…)” Which I presume will miss the Prefect plumbing somehow. I saw someone working on something similar in GitHub issues, and that they’d had to do a double function wrap: https://github.com/PrefectHQ/prefect/issues/10979#issuecomment-2379308965 (I admit I haven’t just tried it yet, which I should do when I’m near my machine and not just reading about Prefect, but I also wanted to know if others were doing this sort of thing and had established best practices for invoking tasks on Modal)
But from what you are telling me, as long as I eventually invoke a prefect decorated function, even if that’s running on a remote machine, things get hooked up/reported if the env variables are there . That makes me think I could either make a wrapper function manually in a worst case or create my own decorator to auto-create the prefect decorated wrapped function.
n
yeah that sounds right. i will say that my impulse is to avoid stacking decorators bc of the cognitive overhead / enmeshing of implementation details between libraries, so my personal recommendation would be: just call the prefect decorated thing inside the function body of the modal decorated thing but yeah that should be not so bad to express with a decorator if you wanted. also on this specific issue you linked, i think we may have fixed the motivating issue here anyways which was some clashing use of
inspect
related to computing cache keys. i’ll go back and see if we can close this issue when im at my keyboard (as well 🙂)
t
I like your instinct on the stacking thing. I’m glad the mental model of “just call a prefect decorated function and prefect will take care of it” is accurate. That’s a powerful primitive I can work with.
catjam 1