当前位置:网站首页>dflow入门5——Big step & Big parameter
dflow入门5——Big step & Big parameter
2022-08-03 08:15:00 【frank_haha】
为了梳理学习dflow时遇到的知识点,我决定开这一个系列记录自己的学习过程。当然了,最好是去看 官方教程 和 文档
本文,我们将讨论dflow的两个feature,big step 和 big parameter
big step
事实上,我们在上一节已经涉及到了Steps的一些用法
具体来说,他可以做循环、步骤重用和条件判断等等
事实上,steps还有一个潜在的用法,就是打包小的templates,实现模块化
我们来看test_big_parameter.py
import time
from dflow import InputParameter, OutputParameter, Step, Steps, Workflow
from dflow.python import (OP, OPIO, BigParameter, OPIOSign, PythonOPTemplate,
upload_packages)
if "__file__" in locals():
upload_packages.append(__file__)
class Hello:
def __init__(self, msg):
self.msg = msg
class Duplicate(OP):
def __init__(self):
pass
@classmethod
def get_input_sign(cls):
return OPIOSign({
'foo': BigParameter(Hello)
})
@classmethod
def get_output_sign(cls):
return OPIOSign({
'foo': BigParameter(Hello)
})
@OP.exec_sign_check
def execute(
self,
op_in: OPIO,
) -> OPIO:
foo = op_in["foo"]
print(foo.msg)
foo.msg = foo.msg * 2
op_out = OPIO({
"foo": foo
})
return op_out
首先定义了template,接收一个自定义参数,然后做一些操作
wf = Workflow(name="big-param")
steps = Steps(name="hello-steps")
steps.inputs.parameters["foo"] = InputParameter()
steps.outputs.parameters["foo"] = OutputParameter()
step1 = Step(
name="step1",
template=PythonOPTemplate(Duplicate, image="python:3.8"),
parameters={
"foo": steps.inputs.parameters["foo"]},
key="step1"
)
steps.add(step1)
step2 = Step(
name="step2",
template=PythonOPTemplate(Duplicate, image="python:3.8"),
parameters={
"foo": step1.outputs.parameters["foo"]},
key="step2"
)
steps.add(step2)
steps.outputs.parameters["foo"].value_from_parameter = \
step2.outputs.parameters["foo"]
随后定义了空的steps,两个子的step串联,再压到steps里面,最后把steps的输出定向为最后一个串联模块的输出
下面一句是它的初始化
big_step = Step(name="big-step", template=steps,
parameters={
"foo": Hello("hello")})
可以看到,big step 的初始化和普通的小step的初始化基本一致,区别在于template的参数不再是PythonOPTemplate,因为steps本身就是一个OP,直接继承了OP,和PythonOPTemplate是等价的
因此,big step 完全可以当做一个 step 去使用!!
这将有利于我们构建更长、更复杂的工作流
Big parameter
上面的例子已经提到了。Big parameter 是自定义的参数,可以丰富变量类型。让OP跟普通python函数的接轨更加顺滑。
边栏推荐
猜你喜欢
随机推荐
QImage的指针问题
How does Mysql query two data tables for the same fields in two tables at the same time
mysql 8.0.12 安装配置方法并--设置修改密码
内存模型之可见性
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
牛客 - 最佳直播时间 (差分)
ArcEngine(五)用ICommand接口实现放大缩小
sqlserver2019安装失败
Nacos使用实践
用云机器/虚拟机架设方舟游戏?
redis stream 实现消息队列
Laya中关于摄像机跟随人物移动或者点击人物碰撞器触发事件的Demo
frp: open source intranet penetration tool
推荐系统-排序层-模型:Wide&Deep
LAN技术-2免费ARP
HCIP练习02(OSPF)
LeetCode 每日一题——622. 设计循环队列
Using pipreqs export requirements needed for the project. TXT (rather than the whole environment)
Redis的基础与django使用redis