Queuelib

1.9.0 · active · verified Fri Apr 10

Queuelib is a Python library providing a collection of persistent (disk-based) and non-persistent (memory-based) queue implementations. It's often used in projects requiring efficient data storage and retrieval, particularly in contexts like web crawling (e.g., Scrapy). The current version is 1.9.0, and it maintains an active, though irregular, release cadence, primarily focusing on Python version compatibility and minor enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating and using a `FifoDiskQueue` for persistent storage. It shows pushing and popping items, closing the queue for integrity, and reopening it to retrieve previously stored data. Remember to always call `.close()` on disk-based queues and handle the storage directory appropriately.

import tempfile
import shutil
from queuelib.disk import FifoDiskQueue

# Create a temporary directory for the queue files
qdir = tempfile.mkdtemp()
try:
    q = FifoDiskQueue(qdir)
    print(f"Queue created at: {qdir}")

    q.push(b'item1')
    q.push(b'item2')
    print(f"Pushed: item1, item2. Size: {len(q)}")

    item = q.pop()
    print(f"Popped: {item}. Size: {len(q)}")

    q.close() # IMPORTANT: Always close disk queues to ensure data integrity
    print("Queue closed.")

    # Reopen the queue from the same directory to demonstrate persistence
    q2 = FifoDiskQueue(qdir)
    item2 = q2.pop()
    print(f"Reopened queue. Popped: {item2}. Size: {len(q2)}")
    q2.close()

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    shutil.rmtree(qdir) # Clean up the temporary directory
    print(f"Cleaned up directory: {qdir}")

view raw JSON →