Skip to main content

High-performance persistent FIFO queues with LMDB backend supporting both single-queue and multi-queue broadcast patterns.

Project description

nque - Persistent FIFO Queue Implementation in Python

Version Python Coverage License

nque is a Python library that implements persistent FIFO queues backed by LMDB (Lightning Memory-Mapped Database). Suitable for building data pipelines and handling data exchange between producer and consumer processes. The queues operate in memory for performance while persisting data to disk for reliability. For detailed API information and usage examples, please refer to the class and method docstrings.

Features

  • Transaction-safe operations
  • Two queue implementations:
    • Basic FIFO queue (FifoBasicQueueLmdb) for single queue scenarios
    • Multi-queue system (FifoMultiQueueLmdb) supporting multiple named queues with broadcast capability
  • Configurable queue size and item size limits

Requirements

  • Python 3.10+
  • LMDB

Installation

pip install nque

Usage Examples

Basic FIFO Queue

from nque import FifoBasicQueueLmdb

# Initialize queue with max 1000 items of max 20 KB each
queue = FifoBasicQueueLmdb(
    db_path="queue", 
    items_count_max=1000,
    item_bytes_max=20 * 1024
)

# Add items
queue.put([b"item1", b"item2"])

# Get items without removing (peek)
items = queue.get(2)

# Remove items after successful processing 
queue.remove(2)

# Get and remove items atomically
items = queue.pop(2)

Multi-Queue System

from nque import FifoMultiQueueLmdb

# Create producer that broadcasts to all internal named queues
producer = FifoMultiQueueLmdb(
    db_path="multiqueue",
    queues=(b"queue1", b"queue2")
)

# Create consumer for specific internal queue
consumer = FifoMultiQueueLmdb(
    db_path="multiqueue",
    queues=(b"queue1", b"queue2"),
    use=b"queue1"
)

# Broadcast to all queues atomically
producer.put([b"broadcast_message"])

# Consume from specific queue
items = consumer.pop(1)

Error Handling

The library provides three exception types:

  • QueueError: Base exception for general queue operations
  • ArgumentError: Invalid argument errors
  • TryLater: Temporary condition preventing operation completion

Performance Considerations

  • Queue max size is pre-defined using max items and item size
  • Write operations (both producers and consumers) are serialized through LMDB's internal locking

API Reference

Common Queue Parameters

  • db_path: Path to the LMDB database storing the queue data
  • items_count_max: Maximum number of items in the queue (default: 1000)
  • item_bytes_max: Maximum size of each item in bytes (default: 20KB)

Multi-Queue Additional Parameters

  • queues: Names of internal queues producers broadcast to
  • use: Name of queue to consume from (for consumers only)

Common Methods

  • put(items): Add items to the queue
  • get(items_count): Retrieve items without removing
  • remove(items_count): Remove items from the queue
  • pop(items_count): Atomic get and remove operation

Best Practices and Error Prevention

  1. Use get() to retrieve items first, then call remove() after successful processing
  2. Use pop() for atomic operations, particularly when concurrent consumers are needed
  3. Implement retry logic for TryLater exceptions

Limitations

  • Write operations (including from consumers) are serialized
  • Get/remove operation pattern requires single consumer per queue
  • Multi-queue supports maximum of 10 internal queues
  • LMDB constraints apply to overall performance

Future Development

The nque library is designed with extensibility in mind. Future development areas may include:

  • Support for monitoring and metrics capabilities
  • Additional queue patterns and implementations
  • Support for additional backend storage systems beyond LMDB

Suggestions and contributions are welcome.

Author

Artur Khakimau

License

MIT

Project details


Download files

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

Source Distribution

nque-1.0.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

nque-1.0.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file nque-1.0.2.tar.gz.

File metadata

  • Download URL: nque-1.0.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for nque-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e6cae7bf306a175ca31e9976ef58837fe2c13b6763ef3d5691307edae63a27d5
MD5 76a4ca05e0b51a198772fdd8428a9c88
BLAKE2b-256 738fab36e2dbbd61c5102a771a2b89286c85b5cddf5940f767c2889562c02019

See more details on using hashes here.

File details

Details for the file nque-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: nque-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for nque-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aa81570e861f548b853d1bbe1e693511eb2c421f47be3d104bfdf1ba62aa5f45
MD5 c284d2bd261fc1f81bf5c256ea7017e4
BLAKE2b-256 fb65809d731b90869ab873ca44f775338350ab286f724ccbd55623c34980cc0f

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