Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

femtoqueue-0.4.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

femtoqueue-0.4.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file femtoqueue-0.4.1.tar.gz.

File metadata

  • Download URL: femtoqueue-0.4.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.6

File hashes

Hashes for femtoqueue-0.4.1.tar.gz
Algorithm Hash digest
SHA256 29376105ac848fea4cea44276622ddb9d6f2edfc93fcf4c57d290fec80015c39
MD5 3ba9eb83fb406e989d473397a629f1d9
BLAKE2b-256 cf0e342c91734bdc3f0464f4832befa541e347a191bd86652036df988fc7cc4e

See more details on using hashes here.

File details

Details for the file femtoqueue-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: femtoqueue-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.6

File hashes

Hashes for femtoqueue-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11fc9cb0e71e8fa57de089c8f43dacb193ccaae6a81498624ece90a41cdb0573
MD5 f95c549581467f8cbaa469f3289e05d8
BLAKE2b-256 213aaffc100de0e4ddf399a1f7fed331553a7964dd77d9819912ed7e3cfcd2d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page