Hello. Has anyone tried running the example code that uses Prefect to execute background tasks? https://docs-3.prefect.io/v3/develop/deferred-tasks
I'm running into errors running the example code. For example:
from prefect.task_worker import serve
I'm getting ModuleNotFoundError: No module named 'prefect.task_worker'
I found that I can import
task_server
instead of `task_worker`:
from prefect.task_server import serve
but then when I try to schedule a deferred task using
my_background_task.delay()
, I get:
AttributeError: 'Task' object has no attribute 'delay'
Has the API changed for background tasks?
Yep -- right after I posted I saw that I had version 2 dot something. which is weird because I created a brand new venv for this. So not sure why it didn't install the latest one. I upgraded and the errors went away
n
Nate
02/12/2025, 6:34 PM
catjam
t
Tony G
02/12/2025, 11:21 PM
Hi @Nate. Thanks for your help. I'm wondering if it's possible to, instead of setting up a worker using
prefect.task_worker
to run background tasks, submit the background task to an already-existing work queue that you have configured in Prefect Cloud?
n
Nate
02/12/2025, 11:25 PM
well the whole worker / work pool paradigm requires you to create deployments, you can’t submit a task by itself to a work pool. but whether you’d choose dynamically dispatched deployments (work pools) or background tasks depends on what you’re doing i’d say
Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.