from prefect.engine.state import Success my_state = Success()
from prefect import task from prefect.engine.state import Success, Failed @task def read_parse_s3_file(bucket_name: str, file_name: str) -> DataFrame: # try to read and parse the file from S3 try: df = pd.load_csv(f's3://{bucket_name}/{file_name}') return df except Exception: raise Failed("Failed to read file from S3") my_state = read_parse_s3_file.run(bucket_name='my-bucket', file_name='data.csv') if isinstance(my_state.result, Success): print("File read and parsed successfully!")
from prefect import task from prefect.engine.state import Success, Failed @task def train_model(x_train: DataFrame, y_train: Series, model_params: dict) -> Model: # try to train the model on the given data try: model = create_model(**model_params) model.fit(x_train, y_train) score = model.score(x_train, y_train) if score >= 0.9: return model else: raise ValueError("Model accuracy is lower than 0.9.") except Exception: raise Failed("Failed to train model") data = load_data() model_params = {'max_depth': 5, 'n_estimators': 100} my_state = train_model.run(x_train=data['X_train'], y_train=data['y_train'], model_params=model_params) if isinstance(my_state.result, Success): print("Model trained successfully!")In both examples, `Success` acts as a signal that the respective task has completed successfully and can proceed to the next task in the workflow, without triggering any error or retry handlers.