Matej
05/16/2021, 8:10 AMfrom prefect import task
@task
def add(x, y):
return x + y
add(1, 2) # 3
this example throws me:
ValueError: Could not infer an active Flow context while creating edge
and suggests using add.run() instead. In the tutorial however, they use add(1,2) regularly.
Are the tutorials outdated for the newest version of prefect?
edit: I am also unable to use .map as it is used in the tutorial:
add.map([1, 10], [2, 3])
throws the same error? How am I supposed to experiment with tasks without flow context?Anna Geller (old account)
05/16/2021, 11:53 AMrun()
method. If you run it in a flow context (with Flow("flowname")...
), then this run() method is called automatically. But when you run it outside of a Flow context, you need to add the run method explicitly like so: add.run(1, 2)
. Here is a slightly adjusted example from the docs:
from prefect import task, Flow
@task
def add(x, y):
return x + y
@task(log_stdout=True)
def compute_sum(nums):
total = sum(nums)
print(f"total = {total}")
return total
@task(log_stdout=True)
def print_map_output(nums):
print(f"Mapping results: {nums}")
with Flow("mapping-example") as flow:
addition_result = add.map([1, 10], [2, 3]) # [3, 13]
print_map_output(addition_result) # if you want to just see the map output
compute_sum(addition_result) # if you want to do something with the map output
if __name__ == '__main__':
add.run(1, 2) # standalone task run
flow.run() # run in a flow context