Syméon del Marmol
08/25/2025, 6:15 PMNate
08/25/2025, 6:17 PMBrendan Dalpe
08/25/2025, 6:17 PMSyméon del Marmol
08/25/2025, 7:14 PMNate
08/25/2025, 7:17 PM» ipython
#[1]
from prefect import flow, task
#[2]
@flow
def compose_some_work(): ...
@task
def serve_this_someplace():
compose_some_work()
#[3]
serve_this_someplace.delay()
14:16:58.593 | INFO | prefect.tasks - Created task run 'serve_this_someplace'. View it in the UI at <URL>
Out[3]: <prefect.futures.PrefectDistributedFuture at 0x115713230>
#[4]
serve_this_someplace.serve()
14:17:04.113 | INFO | prefect.task_worker - Starting task worker...
14:17:04.113 | INFO | prefect.task_worker - Subscribing to runs of task(s): serve_this_someplace
14:17:04.480 | INFO | prefect.task_worker - Received task run: 068acb6a-a807-716a-8000-98335c7b2833 - serve_this_someplace
14:17:04.482 | INFO | prefect.task_worker - Submitting task run 'serve_this_someplace' to engine. View in the UI: <URL>
14:17:05.857 | INFO | Flow run 'armored-wrasse' - Beginning flow run 'armored-wrasse' for flow 'compose-some-work'
14:17:05.862 | INFO | Flow run 'armored-wrasse' - View at <URL>
14:17:06.040 | INFO | Flow run 'armored-wrasse' - Finished in state Completed()
14:17:06.048 | INFO | Task run 'serve_this_someplace' - Finished in state Completed()
^C14:17:11.921 | INFO | prefect.task_worker - Task worker interrupted, stopping...
im delaying the task run and then serving it here just because im in ipython
but typically you'd .delay
from your application or workflow foreground and keep .serve()
going on some static infra (2 separate processes, at least). background tasks scale nice horizontally, like redis stream consumer groups
so in summary:
• serve tasks to start a worker (websocket, does not poll) that will get pushed runs from tasks its serving (ie subscribed to)
• those tasks can call tasks or flows or any python, flows you call will still show up as any other would in the UI
> I tried deployments, but I didn't find simple way to "just" call a flow from our main backend
yea deployments can be overkill if you don't need per-flow-run configuration of your infrastructure (e.g. kubernetes pod with X mem and Y cpu) and you're just trying to make something happen in the backgroundSyméon del Marmol
08/25/2025, 7:53 PMNate
08/25/2025, 7:57 PMreplicas
of the entrypoint that runs the serve process, ie add more workers. you should not run into race conditions related to work distribution (which sometimes happens with the traditional workers that poll for scheduled deployment runs)
https://github.com/PrefectHQ/examples/blob/main/apps/background-tasks/Dockerfile#L40
https://github.com/PrefectHQ/examples/blob/main/apps/background-tasks/compose.yamlNate
08/25/2025, 8:00 PMreplicas: n
means i want N separate websocket clients, each of which can execute task runs its pushed concurrently. so really just add replicas if memory becomes a problem in your workloadsSyméon del Marmol
08/25/2025, 8:03 PM