-
TLDRI've built up a python package that implements class-based workflows, and these workflows can be wrapped into prefect flow objects. However, with the new depolyments api, I am unsure how to register these dynamically-created workflows. Full topicHi all, I've been posting around on slack and several github issues, but I thought I would open up this discussion thread to localize things. I'm sorry if this is overkill, but I could really use some help with the new deployments format. Here's a minimal example of how my package currently works. Let's say we have an abstract base workflow class: from prefect.flows import Flow
class AbstractClassBasedFlow:
"""
An example abstract class that shows how you can dynamically create prefect
flows.
"""
factor: int = None
extra: int = None
@classmethod
def run_config(cls, x, y):
"""
this is what you would normally have as a function and decorate with
prefect's @flow decorator, but here we want to access class methods and
attributes!
"""
a = cls.example_method(x)
b = cls.example_method(y)
return a + b + cls.extra
@classmethod
def example_method(cls, value):
"""
a method that the
"""
return value ** cls.factor
@classmethod
def to_prefect_flow(cls):
"""
dynamically creates a prefect flow for us!
"""
return Flow(
fn=cls.run_config,
name=cls.__name__,
) Having a class like this make it really powerful to build workflows that share methods and also build out other higher-level features. Our current workflow doesn't do much, but it does show us how inheritance can become useful: class Example1(AbstractClassBasedFlow):
factor = 3
extra = 3
class Example2(AbstractClassBasedFlow):
factor = 2
extra = 78
class Example3(AbstractClassBasedFlow):
factor = 5
extra = 123
@classmethod
def example_method(cls, value):
""" example of overriding the parent method"""
return value // cls.factor
# I've now created three "complex" but similar flows using inheritance
flow1 = Example1.to_prefect_flow()
flow2 = Example2.to_prefect_flow()
flow3 = Example3.to_prefect_flow()
# and all workflows behave as normal prefect flows
result1 = flow1(x=3, y=4)
result2 = flow2(x=3, y=4)
result3 = flow3(x=3, y=4)
# I was able to dynamically create deployments for these in prefect v1 and v2beta
# but am now not sure how. Is there any way to do this? How would I create deployments for In practice, the actual |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
@jacksund we are working on further improving the deployment UX and making it clearer how to apply it to various use cases and deployment patterns/environments; I'd recommend waiting for official recipes coming in the next days and weeks before committing to a deployment story just yet. Flows are intended to be used as decorators - could you explain what do you mean when you say generating flows dynamically? do you mean running flows dynamically or building some factory function to avoid boilerplate for flows with similar functionality but e.g. requiring different parameters? |
Beta Was this translation helpful? Give feedback.
-
I put together a little deployment maker class that works at the minute but contains things that are to be deprecated (namely FlowScripts) but might be something that can be used in the interim while things are progressed/finalised. |
Beta Was this translation helpful? Give feedback.
-
This is now possible in v2.1.0. You'd use the |
Beta Was this translation helpful? Give feedback.
This is now possible in v2.1.0. You'd use the
Deployment.build_from_flow
method (api docs). Big thanks to the prefect team for adding this!