Pascal Jahan Elahi (Pawsey)
01/20/2025, 6:52 AMMarvin
01/20/2025, 6:52 AMtask_run.id
attribute from the task run object. Here's a brief explanation:
1. When you submit a task, Prefect creates a task run object.
2. You can access the task run's ID using task_run.id
.
Here's an example of how you might retrieve the task run ID:
python
from prefect import flow, task
from prefect.client import get_client
@task
async def my_async_task():
# Task logic here
pass
@flow
async def my_flow():
task_run = await my_async_task.submit()
print(f"Task Run ID: {task_run.id}")
# To execute the flow
my_flow()
In this example, the submit()
method is used to run the task asynchronously, and task_run.id
provides the ID of the task run.
For more details, you can check the Prefect documentation and task worker implementation.Pascal Jahan Elahi (Pawsey)
01/20/2025, 6:57 AMawait my_async_task.submit()
returns a PrefectFuture
instance and this does not have id
. How can I get the id of a running task?Marvin
01/20/2025, 6:57 AMPrefectFuture
instance, you can use the task_run_id
property. Here's how you can do it:
python
from prefect import flow, task
@task
async def my_async_task():
# Task logic here
pass
@flow
async def my_flow():
future = await my_async_task.submit()
task_id = future.task_run_id
print(f"Task Run ID: {task_id}")
# To execute the flow
my_flow()
The task_run_id
property of PrefectFuture
provides the ID of the task run associated with the future. You can find more details in the Prefect source code.