Is there a way to get the `task_run_id` for a task...
# ask-community
n
Is there a way to get the
task_run_id
for a task that is started from a
flow
run? My use case is this: I want to fetch the logs from a particular task after it was finished and upload it to a DB for access in another tool. e.g.
Copy code
@task
def my_task():
  // do something
  // return something

@flow
def my_flow():
  x = my_task()
  my_task_run_id = ???
  // now fetch the logs for this task
  response = <http://requests.post|requests.post>(
        f'<https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}/logs/filter>',
        headers={'Authorization': f"Bearer {os.environ.get('PREFECT_API_KEY')}"},
        json={'logs': {'task_run_id': {'any_': [my_task_run_id]}}},
    )
1
Alternatively, is there another way to fetch the logs from a task that was started in a flow?
j
You can do something like this:
Copy code
from prefect import flow, task


@task
def my_task():
    return "Task return val"


@flow(log_prints=True)
def my_flow():
    res = my_task.submit()
    # make sure to call result to get the return value
    # and to ensure that submit is finished
    return_value = res.result()
    print(f"got return value: {return_value} from task_run_id {res.task_run.id}")
thank you 1
n
Thanks a lot @Jake Kaplan!A followup question: how long after a task is finished can we expect the logs to be readable in prefect cloud?
j
usually every couple second, but it can depend on a lot of factors, size of logs etc, network speed etc.