Hi! Whenever I rerun a flow, it reuses values from...
# prefect-server
f
Hi! Whenever I rerun a flow, it reuses values from the previous run for the parameters declared globally. How should I handle this?
#!/usr/bin/python3
from datetime import datetime
from prefect import task, Flow, Parameter, context, case
from prefect.utilities.debug import is_serializable
logger = context.get("logger")
timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
@task
def print_timestamp():
<http://logger.info|logger.info>(timestamp)
with Flow("Test") as flow:
    
print_timestamp()
serializable = is_serializable(flow)
if serializable:
    
flow.register(project_name="Default",
                  
idempotency_key=flow.serialized_hash())
flow.run()
Attached screenshot from two runs. As you could see, the value of the global parameter
timestamp
hasn't changed
d
Hi @fblaze f! Since you’re creating that timestamp at Flow build time (i.e. as part of the definition outside of a Task), that value won’t be re-created when the Flow is run. If instead you create the timestamp inside of a Task, it should behave as you expect
👍 1
For example
Copy code
@task
def create_timestamp():
  return datetime.now().strftime("%Y-%m-%dT%H-%M-%S")

@task
def print_timestamp(timestamp):
   <http://logger.info|logger.info>(timestamp)
 
with Flow("test") as flow:
  ts = generate_timestamp()
  print_timestamp(ts)
Task execution is what’s deferred for runtime
Let me know if this helps 😄
f
Thank you. That helps!
d
Great! marvin