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?

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

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:

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 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.1.6.tar.gz (8.5 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.1.6-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for femtoqueue-0.1.6.tar.gz
Algorithm Hash digest
SHA256 ec179ca4a50a031a2190f9b41b43a92ad767159085d902253106b343abbacdd6
MD5 239ef70314001752f1a5b51a8a733781
BLAKE2b-256 afca78071a4ef96a35c6f40f2012e2fb69af2590c83c0d187d2716014f6f2e1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for femtoqueue-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e99457bc307a81d7bce9ca7688d86f9c7cd589583a8ab46ddf6330c6a360b808
MD5 95ad6b85dcd089446b77519956976432
BLAKE2b-256 ba95f489e1281c0e615086e3e68d2c8dd947f71f27b85f99e98d1dc0d524759c

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