Skip to main content

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

Metadata

Release files for nque 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nque 1.0.2
File Size Uploaded
nque-1.0.2.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nque 1.0.2
File Interpreter ABI Platform
nque-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / nque-1.0.2.tar.gz

Download URL nque-1.0.2.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e6cae7bf306a175ca31e9976ef58837fe2c13b6763ef3d5691307edae63a27d5
BLAKE2b-256 checksum
How to use checksums
738fab36e2dbbd61c5102a771a2b89286c85b5cddf5940f767c2889562c02019
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / nque-1.0.2-py3-none-any.whl

Download URL nque-1.0.2-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa81570e861f548b853d1bbe1e693511eb2c421f47be3d104bfdf1ba62aa5f45
BLAKE2b-256 checksum
How to use checksums
fb65809d731b90869ab873ca44f775338350ab286f724ccbd55623c34980cc0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page