register

Applicability

Product

Supported

Atlas A3 training products/Atlas A3 inference products

√

Atlas A2 training products/Atlas A2 inference products

√

Atlas 200I/500 A2 inference products

x

Atlas inference products

x

Atlas training products

x

Function Description

Registers the serialization, deserialization, and size calculation functions corresponding to the user-defined type. This function can be used together with the feed and fetch APIs to feed or fetch any Python type.

Prototype

1
register(msg_type, clz, serialize_func, deserialize_func, size_func=None)

Parameters

Parameter

Data Type

Value Description

msg_type

int

Registered type ID.

clz

Type definition

Type definition, such as int, str, or customized class.

serialize_func

function

Serialization function. The input can be any Python object, and the output is data of the bytes type, that is, the byte stream after the object is serialized.

deserialize_func

function

Deserialization function. The input type is bytes, indicating the byte stream to be deserialized. The output is the deserialized object. It can be any Python object type.

size_func

function

Function for calculating the memory size after serialization, in bytes. This field is reserved.

Returns

None is 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
import cloudpickle
import dataflow as df

class TestClass():
    def __init__(self, name, val):
        self.name = name
        self.val = val

df.msg_type_register.register(1026, TestClass, lambda obj: cloudpickle.dumps(obj), lambda buffer: cloudpickle.loads(buffer))

Constraints

None