register

Applicable Products

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

ID of the type to be registered.

clz

Type definition

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

serialize_func

function

Serialization function. It accepts any Python object as input and outputs bytes, which refers to the byte stream generated after the object is serialized.

deserialize_func

function

Deserialization function. It takes bytes as input, representing the byte stream to be deserialized, and outputs the deserialized object, which can be any type of Python objects.

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