Queuelib
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
- breaking Queuelib frequently removes support for older Python versions. Ensure your environment meets the `requires_python` specification for the installed version.
- gotcha Disk-based queues (e.g., `FifoDiskQueue`, `LifoDiskQueue`, `PriorityDiskQueue`) *must* be explicitly closed using the `.close()` method to ensure all pending writes are flushed to disk and file handles are released. Failure to close can lead to data loss or corruption, especially during unexpected program termination.
- gotcha The `peek()` method, which allows viewing the next item without removing it, was added in version 1.6.0. Code relying on `peek()` will fail on older `queuelib` versions.
- gotcha Be mindful of the distinct import paths for different queue types. `PriorityQueue` (in-memory) is imported directly from `queuelib`, while persistent disk queues are in `queuelib.disk`, and other memory queues are in `queuelib.queue`. Confusing these paths is a common mistake.
Install
-
pip install queuelib
Imports
- FifoDiskQueue
from queuelib.disk import FifoDiskQueue
- PriorityQueue
from queuelib import PriorityQueue
Quickstart
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}")