Hello. Has anyone tried running the example code t...
# ask-community
t
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?
n
hi @Tony G - I'd suspect you have an old
prefect
version? what does
prefect version
show for you?
Copy code
» uv run --with prefect python -c "from prefect.task_worker import serve; print(serve)"
t
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
catjam
t
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
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
t
Ok thanks again.