Jey Farmer
01/02/2024, 3:40 PMRuntimeError: Tasks cannot be run from within tasks. Did you mean to call this task in a flow?
Is there a way around this issue?Marvin
01/02/2024, 3:41 PMsend_chat_message()
within a task in Prefect 2.x that results in a RuntimeError
. However, the error you're encountering is quite specific: "Tasks cannot be run from within tasks."
Based on the error message, it seems that Prefect is designed to prevent the execution of tasks within tasks, likely to enforce a clear structure of operations where tasks are the smallest unit of execution that should not contain sub-tasks.
If you would like to perform an action such as sending a chat message within a task, you may need to consider a few potential workarounds:
1. Refactor your task: Reorganize your code so that the send_chat_message()
operation is invoked after the task has completed, perhaps in a subsequent task that is dedicated to sending notifications based on the outcome of preceding tasks.
2. Use a flow-level operation: Instead of calling send_chat_message()
within a task, use it at the flow level, where the entire flow's logic can be coordinated and notifications can be sent based on the results of tasks once they finish their execution.
3. Invoke an external script or service: If send_chat_message()
is a function you have control over, you could refactor it to be executed as an external script or through an API call, thereby decoupling it from the Prefect task execution and avoiding nested task calls.
4. Task notifications: Look into whether Prefect 2.x has built-in mechanisms for task notifications that you can leverage rather than manually sending messages from within tasks.
Please note that without more specific details or code examples, these are general suggestions. If none of these solutions address your situation, you may consider reaching out directly to the Prefect community or opening a new issue in the Prefect GitHub repository with a detailed description of your use case and the challenges you're facing.Kevin Grismore
01/02/2024, 3:45 PMsend_chat_message.fn()
to call the the function as if it wasn't decorated with @task
Jey Farmer
01/02/2024, 3:46 PMJey Farmer
01/02/2024, 4:46 PMfrom prefect import flow, task, get_run_logger
from prefect_slack.messages import send_chat_message
from prefect_slack import SlackCredentials
slack_credentials = SlackCredentials.load("creds")
flow_name = "flow-from-flow"
@task
def slack_test():
# this does not work
send_chat_message.fn(
slack_credentials=slack_credentials,
channel="#example-channel",
text="First task example"
)
return "placeholder_value1"
@flow(name=flow_name, log_prints=True)
def flow_from_flow():
logger = get_run_logger()
flow1_var = slack_test()
# this works
send_chat_message(
slack_credentials=slack_credentials,
channel="#example-channel",
text="working"
)
if __name__ == "__main__":
flow_from_flow()
Jack P
01/02/2024, 6:42 PMfn
basically reverts from prefect engine function to basic python async function, and you'd need await then when using fn(). I think