Ensure thread safety for SQLite queues
mastermultithreading=True during initialization to ensure thread safety. MySQL queues are thread-safe by default.repository·master·Indexed 18 days ago
https://github.com/peter-wangxu/persist-queueA thread-safe, disk-based queue library for Python that provides persistent storage using file-based, SQLite, or MySQL backends to survive process crashes and restarts. It features synchronous and asynchronous APIs (Python 3.7+), supporting various queue types including PriorityQueue, UniqueQ, and SQLiteAckQueue, as well as a persistent dictionary implementation via PDict.
multithreading=True during initialization to ensure thread safety. MySQL queues are thread-safe by default.Empty, Full).async with context manager implementation.The library provides several asynchronous queue implementations depending on your storage and ordering requirements:
AsyncQueue: An asynchronous file-based queue, optimized for high-throughput scenarios.AsyncSQLiteQueue: An asynchronous SQLite-based queue, providing better transaction support and query capabilities. This is an alias for AsyncFIFOSQLiteQueue.AsyncFIFOSQLiteQueue: A First-In-First-Out SQLite queue (alias for AsyncSQLiteQueue).AsyncFILOSQLiteQueue: A Last-In-First-Out (LIFO) SQLite queue.AsyncUniqueQ: A SQLite queue that ensures no duplicate items are stored.To get the best performance out of persist-queue, consider the following tips:
auto_commit=False and call task_done() to persist changes.Standard installation may not include all features. Use the following commands to enable specific capabilities:
pip install "persist-queue[async]"pip install "persist-queue[extra]"pip install "persist-queue[async]"
pip install "persist-queue[extra]"When moving from the synchronous API to the asynchronous API, follow these steps:
Queue with AsyncQueue.SQLiteQueue with AsyncSQLiteQueue.async with context managers for queue lifecycle.await keyword before all queue operations.async function.Note: Async queues use the same storage format as sync queues and are compatible (they can read each other's data).
# Sync version
from persistqueue import Queue
queue = Queue("/path/to/queue")
queue.put("data")
item = queue.get()
queue.task_done()
# Async version
from persistqueue import AsyncQueue
async with AsyncQueue("/path/to/queue") as queue:
await queue.put("data")
item = await queue.get()
await queue.task_done()You can benchmark the performance of various queue types (including async) using the built-in benchmarking tool. This allows you to compare sync and async queues on your specific platform.
To run benchmarks directly, use the benchmark/run_benchmark.py script. The first argument specifies the number of items to test (default is 1000), and the second argument specifies the output format: rst (for a reStructuredText table), console, or json.
python benchmark/run_benchmark.py 1000 rstFor asynchronous environments (Python 3.7+), use AsyncQueue (file-based) or AsyncSQLiteQueue (SQLite-based). These should be used with async with to ensure proper lifecycle management.
import asyncio
from persistqueue import AsyncQueue
async def main():
async with AsyncQueue("/path/to/queue") as queue:
await queue.put("async item")
item = await queue.get()
await queue.task_done()
asyncio.run(main())import asyncio
from persistqueue import AsyncSQLiteQueue
async def main():
async with AsyncSQLiteQueue("/path/to/queue.db") as queue:
item_id = await queue.put({"key": "value"})
item = await queue.get()
await queue.update({"key": "new_value"}, item_id)
await queue.task_done()
asyncio.run(main())import asyncio
from persistqueue import AsyncQueue
async def main():
async with AsyncQueue("/path/to/queue") as queue:
await queue.put("async item")
item = await queue.get()
await queue.task_done()
asyncio.run(main())To use the asynchronous persistent queue API, you must install the aiofiles and aiosqlite packages. You can install them individually or via the requirements file.
pip install aiofiles aiosqlite
# OR
pip install -r requirements.txtTo use the asynchronous queue implementations, you must install the async extra. This ensures that necessary dependencies like aiofiles and aiosqlite are included.
Recommended installation:
pip install "persist-queue[async]"Manual dependency installation: If you prefer to manage dependencies manually, ensure you have:
aiofiles>=0.8.0aiosqlite>=0.17.0You can install persist-queue using pip. Depending on your requirements, you may need to install extra dependencies for specific features like MySQL or async support.
pip install persist-queuepip install "persist-queue[extra]"pip install "persist-queue[async]"pip install "persist-queue[extra,async]"aiofiles and aiosqlite.DBUtils and PyMySQL.pip install persist-queueUse Queue for a basic file-based FIFO queue. It uses pickle by default for serialization.
from persistqueue import Queue
q = Queue("my_queue_path")
q.put("item1")
item = q.get()
q.task_done()Use SQLiteQueue or FIFOSQLiteQueue for SQLite-backed storage. You can enable auto_commit=True for immediate persistence.
import persistqueue
q = persistqueue.SQLiteQueue('my_queue.db', auto_commit=True)
q.put('data1')
item = q.get()from persistqueue import Queue
q = Queue("my_queue_path")
q.put("item1")
item = q.get()
q.task_done()