Skip to main content

A lightning-fast, zero-copy, cross-process data store for Python using Apache Arrow and shared memory.

Project description

๐Ÿน ArrowShelf

High-Performance Shared Memory Data Exchange for Python

Python Apache Arrow License Performance

ArrowShelf is a cutting-edge Python library that enables lightning-fast shared memory data exchange between processes using Apache Arrow's columnar format. Perfect for high-performance computing, machine learning pipelines, and distributed data processing.

๐ŸŒŸ Key Features

  • ๐Ÿš€ Zero-Copy Operations: Direct memory access without serialization overhead
  • ๐Ÿ”ง Process-Safe: Thread and multiprocess safe data sharing
  • ๐Ÿ“Š Columnar Efficiency: Optimized for analytical workloads with Apache Arrow
  • ๐ŸŽฏ FAISS Integration: Built-in support for approximate nearest neighbor search
  • ๐Ÿ”„ Automatic Cleanup: Smart memory management with reference counting
  • ๐Ÿ›ก๏ธ Production Ready: Robust error handling and connection management

๐Ÿ“ฆ Installation

pip install arrowshelf

For FAISS integration (optional):

pip install faiss-cpu  # or faiss-gpu for GPU support

๐Ÿš€ Starting the ArrowShelf Server

ArrowShelf requires a server daemon to manage shared memory. Start it before running your applications:

# Start the ArrowShelf server
arrowshelf-server

# Or run in background (Linux/Mac)
arrowshelf-server &

# Windows background (using PowerShell)
Start-Process arrowshelf-server -WindowStyle Hidden

The server will run on localhost:50051 by default.

๐Ÿš€ Quick Start

Setting Up ArrowShelf

  1. Start the server (required):
arrowshelf-server
  1. Basic Usage (in a separate terminal/process):
import arrowshelf
import pandas as pd
import numpy as np

# Create sample data
df = pd.DataFrame({
    'x': np.random.rand(10000),
    'y': np.random.rand(10000),
    'z': np.random.rand(10000)
})

# Store in shared memory
key = arrowshelf.put(df)

# Access from any process
retrieved_df = arrowshelf.get(key)
print(f"Retrieved {len(retrieved_df)} rows")

# Cleanup
arrowshelf.delete(key)

Advanced Zero-Copy Access

import arrowshelf
import numpy as np

# Store data
key = arrowshelf.put(df)

# Get Arrow table for zero-copy operations
table = arrowshelf.get_arrow(key)
x_column = table.column("x").chunk(0).to_numpy(zero_copy_only=True)

# Direct NumPy operations without copying
result = np.mean(x_column)

๐ŸŽฏ Real-World Example: Parallel Nearest Neighbor Search

This example demonstrates how ArrowShelf enables efficient parallel processing with FAISS for approximate nearest neighbor search.

Prerequisites

  1. Start ArrowShelf server:
arrowshelf-server
  1. Install dependencies:
pip install arrowshelf faiss-cpu pandas numpy

Complete Example

import multiprocessing as mp
import threading
import pandas as pd
import numpy as np
import time
import arrowshelf
import faiss
from multiprocessing.pool import ThreadPool

# Enable thread-based multiprocessing
threading.Pool = ThreadPool

def worker_faiss_search(task_data):
    """Worker function for parallel FAISS nearest neighbor search"""
    key, start_index, end_index = task_data
    
    # Zero-copy access to shared data
    table = arrowshelf.get_arrow(key).combine_chunks()
    x = table.column("x").chunk(0).to_numpy(zero_copy_only=True)
    y = table.column("y").chunk(0).to_numpy(zero_copy_only=True)
    z = table.column("z").chunk(0).to_numpy(zero_copy_only=True)
    
    # Stack coordinates for FAISS
    all_points = np.stack([x, y, z], axis=1).astype(np.float32)
    query_chunk = all_points[start_index:end_index]
    
    # Configure FAISS IVF index
    d = 3  # 3D points
    nlist = 100  # Voronoi cells
    quantizer = faiss.IndexFlatL2(d)
    index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
    
    # Train and populate index
    index.train(all_points)
    index.add(all_points)
    index.nprobe = 10  # Search cells
    
    # Perform approximate k-NN search
    _, distances = index.search(query_chunk, 11)  # k=11 (excluding self)
    avg_distance = np.mean(np.sqrt(distances[:, 1:]))  # Exclude self-distance
    
    return avg_distance

def parallel_nearest_neighbor_demo():
    """Demonstrate parallel processing with ArrowShelf + FAISS"""
    
    # Check ArrowShelf connection
    try:
        arrowshelf.list_keys()
        print("โœ… ArrowShelf server connection OK")
    except arrowshelf.ConnectionError:
        print("โŒ ERROR: ArrowShelf server not running!")
        print("Please start the server first: arrowshelf-server")
        return
    
    # Generate sample 3D points
    num_points = 100_000
    num_cores = 6
    
    print(f"๐Ÿ” Running parallel k-NN search on {num_points:,} 3D points")
    
    # Create dataset
    df = pd.DataFrame(np.random.rand(num_points, 3), columns=['x', 'y', 'z'])
    print(f"๐Ÿ“Š Dataset size: {df.memory_usage(deep=True).sum() / 1024**2:.2f} MB")
    
    # Store in ArrowShelf
    key = arrowshelf.put(df)
    
    # Create tasks for parallel processing
    chunk_size = num_points // num_cores
    tasks = [
        (key, i * chunk_size, (i + 1) * chunk_size) 
        for i in range(num_cores)
    ]
    
    # Execute parallel search
    print(f"โšก Processing with {num_cores} cores...")
    start_time = time.perf_counter()
    
    with ThreadPool(processes=num_cores) as pool:
        results = pool.map(worker_faiss_search, tasks)
    
    duration = time.perf_counter() - start_time
    avg_distance = np.mean(results)
    
    # Results
    print(f"โœ… Average 10-NN distance: {avg_distance:.6f}")
    print(f"๐Ÿš€ Processing time: {duration:.4f} seconds")
    print(f"๐Ÿ”ฅ Throughput: {num_points/duration:,.0f} points/second")
    
    # Cleanup
    arrowshelf.delete(key)
    print("๐Ÿงน Cleanup completed")

if __name__ == "__main__":
    mp.set_start_method('spawn', force=True)
    parallel_nearest_neighbor_demo()

Running the Example

  1. Terminal 1 - Start the server:
arrowshelf-server
  1. Terminal 2 - Run the example:
python nearest_neighbor_demo.py

Expected Output:

โœ… ArrowShelf server connection OK
๐Ÿ” Running parallel k-NN search on 100,000 3D points
๐Ÿ“Š Dataset size: 2.29 MB
โšก Processing with 6 cores...
โœ… Average 10-NN distance: 210.789151
๐Ÿš€ Processing time: 1.0017 seconds
๐Ÿ”ฅ Throughput: 99,830 points/second
๐Ÿงน Cleanup completed

๐Ÿš€ Project Evolution

ArrowShelf has evolved from a simple data sharing concept to a high-performance computing powerhouse. Here's the journey of optimization:

Performance Evolution Timeline

Benchmark Architecture Algorithm Time Improvement
Pickle + Brute Force Slow Data Transfer Brute Force O(nยฒ) 16.7s Baseline
ArrowShelf + Brute Force Fast Data Transfer Brute Force O(nยฒ) 14.5s 13% faster
ArrowShelf + FAISS IndexFlatL2 Fast Data Transfer Optimized Exact Search 1.84s 87% faster
ArrowShelf + FAISS IndexIVFFlat Fast Data Transfer Approximate Search 1.00s 94% faster

๐Ÿ“Š Performance Visualization

Traditional Pickle Approach    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ 16.7s
ArrowShelf + Brute Force      โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ     14.5s
ArrowShelf + FAISS Exact      โ–ˆโ–ˆโ–ˆโ–ˆ                               1.84s
ArrowShelf + FAISS Approx     โ–ˆโ–ˆ                                 1.00s โšก

๐ŸŽฏ Key Milestones

  • ๐Ÿ—๏ธ Phase 1: Foundation - Basic shared memory with Apache Arrow
  • โšก Phase 2: Optimization - Zero-copy operations and efficient data transfer
  • ๐Ÿ” Phase 3: Intelligence - FAISS integration for similarity search
  • ๐Ÿš€ Phase 4: Approximation - IVF indexing for ultimate performance

The evolution demonstrates a 16.7x performance improvement from traditional pickle-based approaches to our current FAISS-optimized implementation.

๐Ÿ“ˆ Performance

ArrowShelf delivers exceptional performance for data-intensive applications:

FAISS Integration Benchmark

  • Dataset: 100,000 3D points (2.29 MB)
  • Operation: Approximate 10-NN search with IVF index
  • Hardware: 6-core parallel processing
  • Result: 1.00 seconds processing time
  • Throughput: ~100K points/second

Key Performance Benefits

  • Zero-Copy Access: Direct memory mapping eliminates serialization overhead
  • Columnar Storage: Optimized for analytical operations and vectorized computations
  • Parallel Processing: Efficient multi-core scaling with shared memory
  • Memory Efficiency: Reference counting prevents memory leaks

๐Ÿ”ง API Reference

Core Functions

# Store data in shared memory
key = arrowshelf.put(data)

# Retrieve data as pandas DataFrame
df = arrowshelf.get(key)

# Retrieve data as Arrow Table (zero-copy)
table = arrowshelf.get_arrow(key)

# List all stored keys
keys = arrowshelf.list_keys()

# Delete data from shared memory
arrowshelf.delete(key)

# Close connection
arrowshelf.close()

Advanced Operations

# Batch operations
arrowshelf.delete_all()  # Clear all data

# Connection management
arrowshelf.is_connected()  # Check connection status

# Memory statistics
arrowshelf.memory_usage()  # Get usage statistics

๐Ÿ› ๏ธ Use Cases

๐Ÿค– Machine Learning

  • Feature Engineering: Share preprocessed datasets across training processes
  • Model Serving: Cache model predictions and intermediate results
  • Hyperparameter Tuning: Efficient data sharing in parallel optimization

๐Ÿ“Š Data Analytics

  • ETL Pipelines: Zero-copy data transformations
  • Distributed Computing: Shared memory for map-reduce operations
  • Real-time Analytics: High-throughput data processing

๐Ÿ”ฌ Scientific Computing

  • Numerical Simulations: Share large arrays between simulation processes
  • Image Processing: Efficient pixel data sharing
  • Geospatial Analysis: Fast coordinate and geometry operations

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Process A     โ”‚    โ”‚   ArrowShelf    โ”‚    โ”‚   Process B     โ”‚
โ”‚                 โ”‚    โ”‚     Server      โ”‚    โ”‚                 โ”‚
โ”‚  put(data) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถโ”‚                 โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ get(key)   โ”‚
โ”‚                 โ”‚    โ”‚  Apache Arrow   โ”‚    โ”‚                 โ”‚
โ”‚                 โ”‚    โ”‚ Shared Memory   โ”‚    โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“‹ Requirements

  • Python 3.7+
  • Apache Arrow
  • pandas
  • numpy

Optional dependencies:

  • FAISS (for nearest neighbor search)
  • multiprocessing support

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Built on Apache Arrow columnar memory format
  • Optimized for FAISS similarity search
  • Inspired by modern high-performance computing needs

๐Ÿ“ž Support


โญ Star this repository if ArrowShelf helps accelerate your data processing workflows!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arrowshelf-2.3.1-py3-none-win_amd64.whl (832.5 kB view details)

Uploaded Python 3Windows x86-64

File details

Details for the file arrowshelf-2.3.1-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for arrowshelf-2.3.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 3a41834069bae8094c98071dde0af4a2268116d8953522af7b88a3dfe479330e
MD5 790f09a406fd29087dc21de90e0c5830
BLAKE2b-256 f9d8aa4f99b2c8dab003266a453f92a92233b4ffc157126f1041bf10b73d90f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page