title: Python Serialization Guide sidebar_position: 0 id: serialization_index license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Apache Fory™ is a blazing fast multi-language serialization framework powered by JIT compilation and zero-copy techniques, providing up to ultra-fast performance while maintaining ease of use and safety.
pyfory provides the Python implementation of Apache Fory™, offering both high-performance object serialization and advanced row-format capabilities for data processing tasks.
pip install pyfory
# Install with row format support (requires Apache Arrow) pip install pyfory[format] # Install from source for development git clone https://github.com/apache/fory.git cd fory/python pip install -e ".[dev,format]"
pyfory provides ThreadSafeFory for thread-safe serialization using a pooled wrapper:
import pyfory import threading from dataclasses import dataclass @dataclass class Person: name: str age: int # Create thread-safe Fory instance fory = pyfory.ThreadSafeFory(xlang=False, ref=True) fory.register(Person) # Use in multiple threads safely def serialize_in_thread(thread_id): person = Person(name=f"User{thread_id}", age=25 + thread_id) data = fory.serialize(person) result = fory.deserialize(data) print(f"Thread {thread_id}: {result}") threads = [threading.Thread(target=serialize_in_thread, args=(i,)) for i in range(10)] for t in threads: t.start() for t in threads: t.join()
Key Features:
Fory instances protected by a lock for thread safetyFory class with identical methodsWhen to Use:
import pyfory from dataclasses import dataclass @dataclass class Person: name: str age: int # Create Fory instance fory = pyfory.Fory(xlang=False, ref=True) fory.register(Person) person = Person("Alice", 30) data = fory.serialize(person) result = fory.deserialize(data) print(result) # Person(name='Alice', age=30)