https://dagster.io/ logo
#ask-community
Title
# ask-community
m

Maverick Humbert

05/09/2022, 12:07 AM
Hey, is there a case where I should better use @job instead of @graph ?
y

yuhan

05/09/2022, 5:23 PM
Hi @Maverick Humbert dagster machinery always executes a job.
@job
is a convenient way to define a job. let’s say you have this piece of code:
Copy code
from dagster import graph, op

@op
def interact_with_server(context):
    ...

@graph
def do_stuff():
    interact_with_server()
you can execute the
do_stuff
graph directly -- because there’s no extra configuration needed in this graph, dagster machinery would wrap this graph to a job and then execute the job. when you want to use features like resource and reuse the graph, you’d do:
Copy code
from dagster import graph, op

@op(required_resource_keys={"server"})
def interact_with_server(context):
    context.resources.server.ping_server()

@graph
def do_stuff():
    interact_with_server()

prod_job = do_stuff.to_job(resource_defs={"server": prod_server}, name="do_stuff_prod")
local_job = do_stuff.to_job(
    resource_defs={"server": local_server}, name="do_stuff_local"
)
but if you don’t want to reuse the graph you can simply do:
Copy code
from dagster import job, op

@op(required_resource_keys={"server"})
def interact_with_server(context):
    context.resources.server.ping_server()

@job(resource_defs={"server": prod_server}) # here
def do_stuff():
    interact_with_server()
m

Maverick Humbert

05/13/2022, 8:39 AM
oh thanks for the example and details !