method
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
Function Description
Enables classes to be run locally or remotely as pipeline tasks in complex scenarios. You can use @pyflow to decorate a class and @method to decorate a function in the class to indicate that the function needs to be run in pipeline mode. You can also use @method to decorate multiple functions in a class to indicate that they need to be run in pipeline mode together. Functions decorated with @method must be involved in the FlowGraph construction. Decorator @method cannot be used to decorate a function that is not to be directly run as a pipeline task, for example, an internal function.
Prototype
1 | @method
|
Parameters
Parameter |
Data Type |
Value Description |
||
|---|---|---|---|---|
num_returns |
int |
Indicates the number of outputs when a decorator is used. If this parameter is not set, only one value is returned by default. You can either use this parameter to set the number of returned values or use type annotations to set the number and type of returned values. |
||
stream_input |
str |
Indicates that the input of the current function is a stream input (that is, the input parameter of the function is a queue). Currently, only the queue type is supported. You can obtain data from the input queue. |
||
choice_output |
function |
Indicates that the output of the current function is optional. Only the output that meets the conditions (the condition is a user-defined function) is returned. For example:
This example indicates that only outputs other than None are returned. |
Returns
The decorated functions are returned in normal scenarios.
The DfException exception is reported in abnormal scenarios. You can obtain error_code and message in DfException to view the error code and error information. For details, see DataFlow Error Codes.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import dataflow as df @df.pyflow class Foo(): def __init__(self): pass # Use num_returns to indicate that the number of outputs is 2. @df.method(num_returns=2) def func1(a, b): return a + b,a - b # Use type annotations to indicate that the number of outputs is 2. @df.method() def func2(a, b) -> Tuple[int, int]: return a + b,a - b # By default, only one value is returned. @df.method() def func3(a, b): return a + b @df.method(stream_input='Queue') def func4(a, b): data1 = a.get() data2 = a.get() data3 = b.get() return data1 + data2 + data3 @df.method(choice_output=lambda e: e is not None) def func5(self, a) -> Tuple[int, int]: return None, a # Non-null values are sent to the corresponding outputs based on the lambda function. |
Constraints
The cloudpickle package of the corresponding Python version must be installed in the environment.
The function modified by @method must be involved in the graph construction process. For the graph construction of a class decorated by @pyflow, the output cannot be used as the input. If the function has default values, edges are required in the graph construction.
In the streaming input scenario, the DataFlow framework does not support data alignment and exception handling.