Skip to main content

A work queue, on top of a redis database, with implementations in Python, Rust, Go, Node.js (TypeScript) and Dotnet (C#).

This is the Python implementations. For an overview of how the work queue works, it's limitations, and the general concepts and implementations in other languages, please read the redis-work-queue readme.

Setup

from redis import Redis
from redis_work_queue import KeyPrefix, WorkQueue

db = Redis(host='your-redis-server')

work_queue = WorkQueue(KeyPrefix("example_work_queue"));

Adding work

Creating Items

from redis_work_queue import Item

# Create an item from bytes
bytes_item = Item(b"[1,2,3]")

# Create an item from a string
string_item = Item('[1,2,3]');

# Create an item by json serializing an object
json_item = Item.from_json_data([1, 2, 3])

# Retreive an item's data, as bytes:
assert bytes_item.data() == b"[1,2,3]"
assert bytes_item.data() == string_item.data()
assert bytes_item.data() == json_item.data()

# Parse an item's data as JSON:
assert bytes_item.data_json() == [1, 2, 3]

Add an item to a work queue

work_queue.add_item(db, item)

Completing work

Please read the documentation on leasing and completing items.

from redis_work_queue import Item

while True:
    # Wait for a job with no timeout and a lease time of 5 seconds.
    # Use work_queue.lease(db, 5, timeout=10) to timeout and return `None` after 10 seconds.
    job: Item | None = work_queue.lease(db, 5)
    assert job is not None
    do_some_work(job)
    work_queue.complete(db, job)

Handling errors

Please read the documentation on handling errors.

from redis_work_queue import Item

while True:
    # Wait for a job with no timeout and a lease time of 5 seconds.
    job: Item = work_queue.lease(db, 5)
    try:
        do_some_work(job)
    except Exception as err:
        if should_retry(err):
            # Drop a job that should be retried - it will be returned to the work queue after
            # the (5 second) lease expires.
            pass
        else:
            # Errors that shouldn't cause a retry should mark the job as complete so it isn't
            # tried again.
            log_error(err)
            work_queue.complete(db, &job)
        continue
    # Mark successful jobs as complete
    work_queue.complete(db, job)

Metadata

Release files for redis-work-queue 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redis-work-queue 0.1.5
File Size Uploaded
redis-work-queue-0.1.5.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for redis-work-queue 0.1.5
File Interpreter ABI Platform
redis_work_queue-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / redis-work-queue-0.1.5.tar.gz

Download URL redis-work-queue-0.1.5.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e3a018d71b586fd9907df0c3fc68153a5fc3511b7bb43c0c7ff30ce49da68996
BLAKE2b-256 checksum
How to use checksums
d41c9ce0a881940407695bb32674790d16a9ca5591b9a30933a06016949f7dd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / redis_work_queue-0.1.5-py3-none-any.whl

Download URL redis_work_queue-0.1.5-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04e48b21dda125dd82b3727fba59b143c5b0c2372d01a406ab62f86fa754103f
BLAKE2b-256 checksum
How to use checksums
fe770776744402d4a3d06412985d8fa517ee4ca01fc90b1958578d937f020239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

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