You are viewing documentation about an older version (0.1.3). View latest version

snowflake.core.task.context.TaskContext

class snowflake.core.task.context.TaskContext(session: Session)

Bases: object

Represents the context in a Snowflake Task.

When a Task’s definition is a StoredProcedureCall, the handler of the Stored Procedure can use this class to set the return value of the Task so the immediate successor tasks can use it.

Methods

__init__(session: Session) None

Initialize a TaskContext object.

Parameters:

session – a Snowpark session.

get_current_task_name() str

Return the name of the task currently executing.

This method can only be called in a Snowflake task.

Example

>>> def task_handler(session: Session, table_name: str) -> str:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     return context.get_current_task_name()
Copy
get_predecessor_return_value(task_name: str | None = None) str

Retrieve the return value from the predecessor task in a DAG of tasks.

The return value is explicitly set by the predecessor task using set_return_value(). This method can only be called in a Snowflake task.

See SYSTEM$GET_PREDECESSOR_RETURN_VALUE for details.

Parameters:

task_name

The task name of the predecessor task that sets the return value to be retrieved.

  • If the task has only one predecessor task that is enabled, the argument is optional. If this argument is omitted, the function retrieves the return value for the only enabled predecessor task.

  • If the task has multiple predecessor tasks that are enabled, this argument is required.

Example

>>> def task_handler(session: Session, table_name: str) -> str:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     pred_return_value = context.get_predecessor_return_value("pred_task_name")
>>>     return pred_return_value
Copy
set_return_value(value: Any) None

Explicitly sets the return value for a task.

An immediate successor task can then use get_predecessor_return_value() to retrieve it. See SYSTEM$SET_RETURN_VALUE for details. This method can only be called in a Snowflake task.

Parameters:

value – The return value for a task. It will be converted to a str when the underlying SQL system function is called.

Example

>>> def task_handler(session: Session, table_name: str) -> str:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     # this return value can be retrieved by successor Tasks.
>>>     context.set_return_value("predecessor_return_value")
>>>     return "Success"
Copy