method
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
Function Description
Enables classes to be run locally or remotely as pipeline tasks in complex scenarios. To achieve this, decorate the class with @pyflow and decorate its internal functions with @method. This indicates that the target functions are to be executed via the pipeline mode. A single class can contain multiple @method-decorated functions, which means they can receive input data and execute concurrently. All functions marked with @method must participate in constructing the FlowGraph. 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 |
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
Decorated functions (in normal scenarios).
A DfException is thrown upon exceptions. You can catch DfException and retrieve its error_code and message attributes to check the specific error code and error details. 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.