Ever wanted a zero-dependency, filesystem-backed, lock-free, durable, concurrent, retrying task queue implementation? No?
Project description
FemtoQueue
Ever wanted a zero-dependency, filesystem-backed, lock-free, durable, concurrent, retrying task queue implementation? No?
Example
from femtoqueue import FemtoQueue, FemtoTask
q = FemtoQueue(data_dir = "fq", node_id = "node1")
q.push("foobar".encode("utf-8"))
while task := q.pop():
# Do something with `task.data`
q.done(task) # or q.fail(task)
print("All tasks processed")
Installation
Just chuck the femtoqueue.py file into your Python 3 project. There are no dependencies other than the standard library.
Features
This mini-library provides the FemtoQueue class with the standard queue interface:
| Method | Description |
|---|---|
push(task: bytes) -> str |
Add a task to the queue, returns id |
pop() -> FemtoTask |
Get a task from the queue |
Each task corresponds to one file in the data_dir directory. State changes are atomic since they use rename(). The task can contain whatever you want, the queue does not inspect it in any way.
Each concurrent worker node (library user) must have a stable identifier node_id. This way workers can automatically retry a task if they unexpectedly crash in the middle of processing.
Stale tasks (i.e. in progress for too long) are moved back to pending automatically after a timeout is reached (default: 30s).
But isn't this slow?
I wouldn't migrate away from your production queue system just yet, but this is faster than you'd expect. Easily fast enough for some small or medium project. Turns out, creating and renaming files is pretty snappy.
Running the microbenchmark python benchmark_mini.py on a Macbook Pro M1 reports around 4500 pushed tasks/sec and 400 popped tasks/sec. Most of the time is spent opening files. A heftier FreeBSD machine was able to reach 21000 pushed/sec and 7400 popped/sec. Note that these are not very scientific numbers.
Unit tests
python test.py
Author and license
Jan Tuomi <jan@jantuomi.fi>. Licensed under AGPL 3.0. All rights reserved.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file femtoqueue-0.1.0.tar.gz.
File metadata
- Download URL: femtoqueue-0.1.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
950f0d72e5228c477a87c741ef76a8623015f3c3af472789ee23e12c9c02ffe7
|
|
| MD5 |
5a3ec20bff17117656bc87ccf613ebe9
|
|
| BLAKE2b-256 |
a43fadd34bf3bcd66acd7cc462edb17a2f380f10660edab4b6bffa3dd8df3a0d
|
File details
Details for the file femtoqueue-0.1.0-py3-none-any.whl.
File metadata
- Download URL: femtoqueue-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88d63bcc6a67405d554584407276900081aa324e35fbec39e53e3b4f6321fc81
|
|
| MD5 |
ca1d59a92e4ca0257fe38a414c5ac69a
|
|
| BLAKE2b-256 |
d473b8dc0341d935303a9ec1f45d7ff1189dc1c286f4d8dc11336c97c52f3f23
|