Skip to content

Conversation

@mt-ob
Copy link
Owner

@mt-ob mt-ob commented Aug 7, 2025

from metaflow import FlowSpec, step

class LoopingFlow(FlowSpec):
    @step
    def start(self):
        print("Flow starting. Initializing loop counter.")
        # This variable will track our position in the loop.
        self.count = 0
        # We will loop 5 times.
        self.max_iterations = 5
        self.next(self.loop_step)

    @step
    def loop_step(self):
        print(f"Executing loop iteration number: {self.count}")
        self.count += 1
        self.loop_status = 'continue' if self.count < self.max_iterations else 'exit'
        print(f"Loop status is '{self.loop_status}'. Deciding next step...")
        self.next(
            {
                'continue': self.loop_step,
                'exit': self.exit_loop
            },
            condition='loop_status'
        )

    @step
    def exit_loop(self):
        print(f"Loop finished after {self.count} iterations.")
        self.next(self.end)

    @step
    def end(self):
        print("Flow has finished successfully.")

if __name__ == '__main__':
    LoopingFlow()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants