Python 尝试使用文件在Kubeflow组件之间传递数据时出现问题

Python 尝试使用文件在Kubeflow组件之间传递数据时出现问题,python,kubeflow,kubeflow-pipelines,Python,Kubeflow,Kubeflow Pipelines,我使用python函数制作了两个组件,并试图使用文件在它们之间传递数据,但我无法做到这一点。我想计算总和,然后使用文件将答案发送给另一个组件。下面是部分代码(代码在不传递文件的情况下工作)。请帮忙 # Define your components code as standalone python functions:====================== def add(a: float, b: float, f: comp.OutputTextFile(float)) ->

我使用python函数制作了两个组件,并试图使用文件在它们之间传递数据,但我无法做到这一点。我想计算总和,然后使用文件将答案发送给另一个组件。下面是部分代码(代码在不传递文件的情况下工作)。请帮忙

# Define your components code as standalone python functions:======================
    def add(a: float, b: float, f: comp.OutputTextFile(float)) -> NamedTuple(
        'AddOutput',
        [
            ('out', comp.OutputTextFile(float))
        ]):
        '''Calculates sum of two arguments'''
        sum = a+b

        f.write(sum)

        from collections import namedtuple

        addOutput = namedtuple(
            'AddOutput',
            ['out'])
        return addOutput(f)  # the metrics will be uploaded to the cloud


    def multiply(c:float, d:float, f: comp.InputTextFile(float) ):
        '''Calculates the product'''
        product = c * d

        print(f.read())


add_op = comp.func_to_container_op(add, output_component_file='add_component.yaml')
    product_op = comp.create_component_from_func(multiply, 
output_component_file='multiple_component.yaml')


@dsl.pipeline(
      name='Addition-pipeline',
      description='An example pipeline that performs addition calculations.'
    )
    def my_pipeline(a, b='7', c='4', d='1'):

        add_op = pl_comp_list[0]
        product_op = pl_comp_list[1]

        first_add_task = add_op(a, 4)
        second_add_task = product_op(c, d, first_add_task.outputs['out'])

这里是我测试的管道的一个稍微简化的版本,它可以工作。 传递给
outputExtFile
inputExtFile
的类类型并不重要。它将被读写为
str
。这就是你应该改变的:

  • 写入
    OutputTextFile
    时:将
    sum
    float转换为str
  • InputTextFile
    读取时:将
    f.read()
    值从
    str转换为float
import kfp
from kfp import dsl
from kfp import components as comp


def add(a: float, b: float, f: comp.OutputTextFile()):
    '''Calculates sum of two arguments'''
    sum_ = a + b
    f.write(str(sum_)) # cast to str
    return sum_


def multiply(c: float, d: float, f: comp.InputTextFile()):
    '''Calculates the product'''
    in_ = float(f.read()) # cast to float
    product = c * d * in_
    print(product)
    return product


add_op = comp.func_to_container_op(add,
                                   output_component_file='add_component.yaml')
product_op = comp.create_component_from_func(
    multiply, output_component_file='multiple_component.yaml')


@dsl.pipeline(
    name='Addition-pipeline',
    description='An example pipeline that performs addition calculations.')
def my_pipeline(a, b='7', c='4', d='1'):

    first_add_task = add_op(a, b)
    second_add_task = product_op(c, d, first_add_task.output)


if __name__ == "__main__":
    compiled_name = __file__ + ".yaml"
    kfp.compiler.Compiler().compile(my_pipeline, compiled_name)