Skip to main content

FemtoQueue

PyPI - Version GitHub Actions Workflow Status

Ever wanted a zero-dependency, filesystem-backed, lock-free, durable, concurrent, retrying task queue implementation? No?

Note: This is pre-release software. Backwards compatibility will be guaranteed after v1.0.

Example

from femtoqueue import FemtoQueue

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

femtoqueue is available on PyPI.

uv add femtoqueue # using uv
pip install femtoqueue # using pip

Or 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, with some additions:

Method Description
push(task: bytes) -> str Add a task to the queue, returns id
pop() -> FemtoTask Get a task from the queue
schedule(task: bytes, time_us: int) -> str Schedule a task for the given timestamp
done(task: FemtoTask) Mark a task as done
fail(task: FemtoTask) Mark a task as failed

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.

Scheduled tasks are moved to the back of the pending queue when the specified wall-clock time is less than the current wall-clock time. Stale tasks (i.e. in progress for too long) are moved back to pending automatically after a timeout is reached (default: 30s). These events are processed during a pop() call; the library does not run any background jobs by itself.

Tasks are ordered using the system-provided monotonic clock to avoid issues such as NTP-related clock skew or daylight savings time moving the clock backwards in time, which would be possible if ordering was based on wall-clock time. As long as all worker nodes have the same clock source, pending tasks are guaranteed to be processed in insertion order, with microsecond precision.

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 Apache-2.0. All rights reserved.

Metadata

Release files for femtoqueue 0.4.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 femtoqueue 0.4.2
File Size Uploaded Provenance
femtoqueue-0.4.2.tar.gz 9.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 19.9 kB

Release files / femtoqueue-0.4.2.tar.gz

Download URL femtoqueue-0.4.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
86eabb2f50282729178262155a0af6ff90e9de167b7ab920a93645aa5a98be58
BLAKE2b-256 checksum
How to use checksums
ac00115c99cdb66d4e40494d0a749eaa2163a4249985420a0e09b57b6c698486
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Release files / femtoqueue-0.4.2-py3-none-any.whl

Download URL femtoqueue-0.4.2-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ec3deaca363d4445c212fbadea8fbac2cdc18cf3ea4c24147631d9494e789cc
BLAKE2b-256 checksum
How to use checksums
c6842f5eb8d7ca31ca0356d49ccf39292af84f13f857b27551a23bc77ab44ab7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Security for femtoqueue 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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